speed test for cookie, localStorage and sessionStorage

OS: Ubuntu 10.10

Chrome 17.0.963.66

data size cookie read cookie write local read local write session read session write
0 1396 7850 1187 1190 1361 1200
64 1333 6929 88 674 2415 1223
128 1368 5509 326 719 3277 1213
192 1393 5947 555 768 4498 1151
256 1621 5442 200 705 5376 1134
320 1325 6113 592 676 6108 1154
384 1399 4503 244 750 6859 719
448 1315 3978 641 736 7720 1176
512 1337 4009 756 712 8679 1106
576 1327 3467 731 701 9541 1116
640 1250 4125 773 683 10710 1111
704 1240 4503 717 700 11506 1027
768 1430 3518 607 695 12256 744
832 1222 3141 905 764 13026 1001
896 1239 3735 820 728 13972 1159
960 1247 2812 773 747 14746 722
1024 1505 2952 791 707 15848 1068

Firefox 10.0.2

data size cookie read cookie write local read local write session read session write
0 31957 1895 12252 7809 22054 22090
64 28641 1890 9448 6492 39672 20430
128 25112 1677 9547 6563 56047 20092
192 28732 1961 9264 7460 74202 21196
256 26139 2011 9560 7129 93047 20911
320 25845 1902 8395 7229 111109 21128
384 26960 1975 9060 6872 127529 20633
448 26334 1895 9473 6885 145823 20728
512 23993 1222 9024 6735 164057 19654
576 23725 1982 9024 6294 182884 19917
640 23752 1900 8527 6382 201092 19128
704 24107 1984 8608 6205 219868 19486
768 19590 1875 8895 6252 237952 19693
832 24222 1886 8579 6593 256189 18980
896 23952 1902 8694 6152 271835 19179
960 21147 1871 8397 5913 289893 18517
1024 20779 1884 8284 5711 308759 17299

Opera 11.61.1250

data size cookie read cookie write local read local write session read session write
0 14617 2936 8768 8129 9222 8793
64 14988 2740 8611 7464 16673 8437
128 13528 2804 8473 8355 25565 8362
192 12425 2690 8627 8562 34037 8619
256 12704 2836 8456 7827 42814 8224
320 12158 2648 8409 8368 51062 7465
384 12472 2619 8262 8084 59452 7638
448 11746 2562 8451 7884 67599 7581
512 11304 2563 8057 8280 75565 7360
576 10736 2655 8323 7921 83545 7611
640 10963 2479 7788 7993 91553 7353
704 10161 2534 8146 7908 99683 7871
768 10453 2399 7872 8029 107597 7128
832 9618 2543 7651 7796 115688 6983
896 9442 2432 7834 7984 123361 7860
960 9225 2480 7520 7643 131320 7662
1024 8689 2408 7702 7955 138966 7436

Замыкания в PHP

Замыкания удобно применять в качестве callback-функций.
Замыкания создают функции, не имеющие определенных имен.
Замыкания автоматически преобразуются в экземпляры класса Closure.
Время жизни переменных, используемых в замыкании, может быть больше, чем у той функции, где их определили.
В контексте класса для передачи $this в замыкание нужно использовать промежуточную переменную:

$v = $this;
use ($v)

Пример (0):

class Test {
public function One() {
$function = 'Two';
$this->$function();
}

public function Two() {
echo 'Two';
}
}

$test = new Test();
$function = 'One';
$test->$function(); // Two

Пример (1):

$move = function($array) {
return $array > 3;
};
var_dump(array_filter(
array(1, 2, 3, 4, 5)
), $move); // array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) } object(Closure)#1 (1) { ["parameter"]=> array(1) { ["$array"]=> string(10) "" } }
$array = array(1, 2, 3, 4, 5);
var_dump(array_filter($array, $move)); // array(2) { [3]=> int(4) [4]=> int(5) }

Пример (2):

$movemax = function($max) {
return function ($array) use ($max) {return $array > $max;};
};
$array = array(1, 2, 3, 4, 5);
var_dump(array_filter($array, $movemax(3))); array(2) { [3]=> int(4) [4]=> int(5) }

Пример (3):

class Test {
public $myfunc;
public function __construct() {
$this->myfunc = function($array){
var_dump($array); // array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) }
};
}
}
$test = new Test();
call_user_func($test->myfunc, array(1, 2, 3, 4, 5));

Пример (4):

$array['myfunc'] = function() {echo 1;};
$array['myfunc'](); // 1

Пример (5):

class Test {
public function MyAction($a, $b, $action) {
switch ($action) {
case 'Add':
return function() use ($a, $b) {
return $a + $b;
};
break;
case 'Sub':
return function() use ($a, $b) {
return $a - $b;
};
break;
}
}
}
$test = new Test();
$func1 = $test->MyAction(3, 2, 'Add');
$func2 = $test->MyAction(3, 2, 'Sub');
echo $func1(); // 5
echo $func2(); // 1

Пример (6):

function myfunc($a, $b, $c) {
echo $a . ' ' . $b . ' ' . $c . "\n";
}
for ( i = 0; i < 5; i++) {
myfunc('hello', 'count', i);
}

или

function myfunc($a, $b, $c) {
echo $a . ' ' . $b . ' ' . $c . "\n";
}
$cmyfunc = function($c) {
myfunc('hello', 'count', $c);
};
for ( i = 0; i < 5; i++) { $cmyfunc($i); } // hello count 0 hello count 1 hello count 2 hello count 3 hello count 4 Замыкания могут взаимодействовать с внешними переменными: $array = array(1, 2, 3, 4, 5); $closure = function() use ($array) { var_dump($array); }; $closure(); // array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) }

Можно передавать переменные по ссылке:

$array = array(1, 2, 3, 4, 5);
$closure = function() use (&$array) {
unset($array[0]);
};
var_dump($array); // array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) }
$closure();
var_dump($array); // array(4) { [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) }

Можно использовать передачу по ссылке, например, для обхода многомерного массива:

$array = array(array (1, 2, array(3, array(4, 5))));
$sum = 0;
array_walk_recursive($array, function($item, $key) use (&$sum) {
$sum += $item;
});
echo $sum; // 15

preg_split и прямой вызов функций внутри замыканий

50000
0.0020001009607315
20001009607315
1884 56.68
0.0016987597370148
16987597370148 15.06
2952
100000
0.0020226094222069
20226094222069
1884
0.0017117062973976
17117062973976 15.37
2952
150000
0.0020203295516968
20203295516968
1884
0.0017234358565013
17234358565013 14.69
2952

Вывод: прямой вызов работает быстрее на 15%, но затрачивает памяти на 56% больше.

ClamAV as a Validation Filter in Zend Framework

here

nginx-http-concat

Модуль взят из tengine от taobao и для объединения вполне удобен!

mod_aclr2

И еще один полезный модуль. Предназначен для отдачи тяжелой статики от Apache 2.x.

модуль
модуль для 1.х

ngx_lua

module
wiki

Редирект для Android, iPhone, iPod

server {
if ($http_user_agent ~* ‘(Android|iPhone|iPod)’) {
rewrite ^/$ http://m.site.org/;
}
}

const, define и память

На простом примере проверялось число байт, затраченных на объявление 500 констант через define и const.

const

const real const emalloc const peak real const peak emalloc
start  524288  370528  524288  431488
stop  524288  370832  524288  431488

define

define real define emalloc define peak real define peak emalloc
start  1048576  454548  1048576  747424
stop  1048576  454852  1048576  747424

Получить данные из PUT

файл

$p = fopen(‘php://input’, ‘r’);
while ($d = fread($p. 1024)) {
…actions…
}

данные

parse_str(fopen(‘php://input’), $p);

Concurrent Caching at Google

http://www.infoq.com/presentations/Concurrent-Caching-at-Google

Rewrite с файла на файл

Требуется:

по запросу /path/to/images/1.gif брать файл /path/to/images/1.gip

Решение:

location ~* \.(gif)$ {
root /path/to/site/path/to/images;
rewrite ([0-9]).gif /$1.gip break;
}