Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Faster - Array vs. Database

I am adding meta tags, titles, stylesheets and scripts to each page via PHP - but all separate from page to page (since i have a header.php that is universal) I am still on the fence though of what is faster for 20+ pages. I could easily store all my values and such in a multidimensional array and return the whole array, then distribute each value accordingly or I can query a database but really which would give me the fastest result? Would it really be that much slower to even matter? Would the user even notice? All questions I have no answers to what do you guys think?

like image 782
Howdy_McGee Avatar asked Nov 30 '25 23:11

Howdy_McGee


2 Answers

I wouldn't bring a database into the mix for a couple dozen pages - not unless you've got a bunch of content you need to store in there. Rather, what I would do is build the array on each page, then include the template:

<?php
  $page = array(
    'title'   => 'Index Page',
    'styles'  => array( 'reset.css', 'text.css', '960.css' ),
    'scripts' => array( 'jquery.js' )
  );

  include('header.php');
?>

  <p>Page content here<?p>

<?php
  include('footer.php');
?>

Within your header.php file, have the following:

<title><?php echo isset($page['title']) ? $page['title'] : 'Default' ; ?></title>
<?php foreach ( $page['styles'] as $style ) : ?>
<link rel='stylesheet' href='<?php echo $style; ?>' />
<?php endforeach; ?>

This may be suitable for much of what you're doing, but when things start to get heavier - definitely put more long-term stock in a database solution.

like image 158
Sampson Avatar answered Dec 02 '25 11:12

Sampson


Neither are nearly as fast as using the filesystem -

1) by separating content into named files identifiable by the client you can take advantage of HTTP's caching mechanisms

2) by not requiring the request to be routed via your application logic (or app logic + database) the I/O operation will have less overhead and therefore be faster.

(note Jonathan Sampson's answer uses a hybrid approach of storing the data in the filesystem but with option of selectively presenting different groups to the browser).

There are other times when you have to manipulate arbitrary data structures within your application where filesystem storage is not appropriate and where you might want to think about the performance of arrays vs databases. IME, PHP arrays are not always faster than DBs - and as the size of the data pool increases, a database becomes massively more efficient (effectively the database has greater bandwidth combined with greater latency). The only to know for sure which is faster / more scalable is to measure it - but if your dataset is less than, say, 100 items then the array will probably be faster.

like image 40
symcbean Avatar answered Dec 02 '25 12:12

symcbean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!