Skip to content

Instantly share code, notes, and snippets.

@nikic
nikic / objects_arrays.md
Last active April 12, 2024 17:05
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:

@ralt
ralt / gist:f4bd9e9133dee42e36a6
Last active April 16, 2018 19:30
The Contributors Army

tl;dr we're lazy, we're coders.

Let's make use of that.

  • We don't like to start off new projects and write all the booooring boilerplate
  • We like to fiddle with code and fix it
  • We like recognition
  • We're quickly bored

Hence, I present to you my idea:

@CS1000
CS1000 / JamExchange.js
Last active July 17, 2017 12:50
JamExchange Room @so Live Radio like Player with "User Requests" (YouTube only, UserScript)
// ==UserScript==
// @name JamExchange Player
// @namespace jamexchange
// @description JamExchange Room @SO Live Radio like Player with "User Requests" (YoutTube only)
// @include http://chat.stackoverflow.com/rooms/39426/*
// @version 1.1.0
// @grant none
// ==/UserScript==
/*
@morrisonlevi
morrisonlevi / loops.php
Last active December 16, 2015 20:29
Empty Loop Micro-benchmarks. Useless except for curiosity.
<?php
define('ITERATIONS', 10000000);
$var = 0;
$start = microtime(true);
$var = 0;
while ($var < ITERATIONS) {
++$var;
}
@hakre
hakre / README.md
Last active September 26, 2015 13:17
Former Print_r Converter Gist, now gone full-blown: https://github.com/hakre/print_r-converter

Print_r Converter

As of September 2013, the repository has been moved from this Github Gist (hakre/1102761) over to a public Github repository in my account. You can find it now here:

Please visit that address for more information, updates and code.

@CS1000
CS1000 / hide_answers.js
Created April 2, 2015 10:29
SNIPPET to hide all answers on a StackExchange post, except the one linked to (hash anchor, eg. #id) if exists.
javascript:(function(){$(".answer").each(function(i,n){if(n.id!==('answer'+document.location.hash).replace(/#/,'-'))n.style.display="none"})})()
@CS1000
CS1000 / colornames.js
Last active August 29, 2015 14:08 — forked from rlemon/colornames.js
For use on Miaou (add "color: <#hexcolor || colorname>" somewhere in your profile->about me)
function hashCode(str) {
var hash = 0;
str += '!';
for (var i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
return -hash;
}
function colorCode(i) {
return '#' + (Math.min((i >> 24) & 0xFF, 200).toString(16) +
/*
* @param rgb = Object {r, g, b}
* @param percent = int (-100 to 100)
*/
function shadeColorTone(rgb, percent)
{
perc = 1 + percent / 100;
ret = {}
Object.keys(rgb).map(function(v) {
col = rgb[v];
@CS1000
CS1000 / log_setContrast.js
Last active August 29, 2015 14:08
Logarithmic contrast
function setContrast(rgb, perc)
{
ret = {}
Object.keys(rgb).map(function(v) {
col = rgb[v];
if (perc <= 0) {
col += (col - 128) * perc / 100; // ---> 128
} else {
if (col < 128) {
//bad start
@CS1000
CS1000 / setContrast.js
Created November 2, 2014 09:44
Linear contrast
function setContrast(rgb, perc)
{
perc = perc / 100;
ret = {}
Object.keys(rgb).map(function(v) {
col = rgb[v];
if (perc <= 0) {
col += (col - 128) * perc; // ---> 128
} else {
if (col < 128) {