Skip to content

Instantly share code, notes, and snippets.

@nikic
nikic / objects_arrays.md
Last active April 24, 2025 06:50
Post explaining why objects often use less memory than arrays (in PHP)

Why objects (usually) use less memory than arrays in PHP

This is just a small post in response to [this tweet][tweet] by Julien Pauli (who by the way is the release manager for PHP 5.5). In the tweet he claims that objects use more memory than arrays in PHP. Even though it can be like that, it's not true in most cases. (Note: This only applies to PHP 5.4 or newer.)

The reason why it's easy to assume that objects are larger than arrays is because objects can be seen as an array of properties and a bit of additional information (like the class it belongs to). And as array + additional info > array it obviously follows that objects are larger. The thing is that in most cases PHP can optimize the array part of it away. So how does that work?

The key here is that objects usually have a predefined set of keys, whereas arrays don't:

@bsalim
bsalim / php speed up tips.html
Created January 3, 2013 09:09
63 Tips for speeding up PHP
<html>
<body>
<p>Here are Webber’s points:</p>
<ul>
<li>If a method can be static, declare it static. Speed improvement is by a factor of 4.</li>
<li>echo is faster than print.(<em>* compare with list from phplens by John Lim</em>)</li>
<li>Use echo’s multiple parameters instead of string concatenation.</li>
<li>Set the maxvalue for your for-loops before and not in the loop.</li>
<li>Unset your variables to free memory, especially large arrays.</li>
<li>Avoid magic like __get, __set, __autoload</li>
@lucasdavila
lucasdavila / object_join.js
Last active February 24, 2023 17:40
Joining javascript key-value objects as string.
Object.prototype.join = function(glue, separator) {
var object = this;
if (glue == undefined)
glue = '=';
if (separator == undefined)
separator = ',';
return Object.keys(object).map(function (key, value) { return [key, value].join(glue); }).join(separator);
@PhirePhly
PhirePhly / smtp_exchange.txt
Created June 11, 2012 21:05
A sample SMTP exchange using CCSMTP
./ccsmtpd[3519]: Starting thread for socket #5
220 kwf.dyndns.org SMTP CCSMTP
C5: EHLO mail-ey0-f180.google.com
S5: 502 Command not implemented
C5: HELO mail-ey0-f180.google.com
S5: 250 Ok
C5: MAIL FROM:<[email protected]>
S5: 250 Ok
C5: RCPT TO:<[email protected]>
S5: 250 Ok
@ScottPhillips
ScottPhillips / .htaccess
Created February 2, 2012 04:30
Common .htaccess Redirects
#301 Redirects for .htaccess
#Redirect a single page:
Redirect 301 /pagename.php http://www.domain.com/pagename.html
#Redirect an entire site:
Redirect 301 / http://www.domain.com/
#Redirect an entire site to a sub folder
Redirect 301 / http://www.domain.com/subfolder/
@kamermans
kamermans / install-mysql-pcre-udf.sh
Created January 22, 2012 20:28
Install MySQL PCRE (Perl-Compatible Regular Expression) UDF on Amazon Linux / CentOS / RedHat / Fedora
#!/bin/bash -e
# UDF Documentation: http://www.mysqludf.org/lib_mysqludf_preg/
yum -y install pcre-devel gcc make automake mysql-devel
wget http://www.mysqludf.org/lib_mysqludf_preg/lib_mysqludf_preg-1.0.1.tar.gz
tar -zxvf lib_mysqludf_preg-1.0.1.tar.gz
cd lib_mysqludf_preg-1.0.1
./configure
make install
echo "You'll need to enter your MySQL password a few times to install the UDFs and test them"