Skip to content

Instantly share code, notes, and snippets.

@usagi
Created April 6, 2012 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save usagi/2318809 to your computer and use it in GitHub Desktop.
Save usagi/2318809 to your computer and use it in GitHub Desktop.
// (c) 2012 Usagi Ito / Wonder Rabbit Project <usagi@WonderRabbitProject.net>. License is MIT/X11.
var v = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
var
a1 = [v],
a2 = [[v]],
a3 = [[[v]]],
a4 = [[[[v]]]],
a5 = [[[[[v]]]]];
var benchmark = function(f, title){
var a = new Date();
for(var n = 100*1000*1000; n; --n)
f();
var b = new Date();
console.log( { time:b-a, title:title } );
};
benchmark( function(){ var a = v; }, 'direct' );
benchmark( function(){ var a = a1[0]; }, 'array depth-1');
benchmark( function(){ var a = a2[0][0]; }, 'array depth-2');
benchmark( function(){ var a = a3[0][0][0]; }, 'array depth-3');
benchmark( function(){ var a = a4[0][0][0][0]; }, 'array depth-4');
benchmark( function(){ var a = a5[0][0][0][0][0]; }, 'array depth-5');
@usagi
Copy link
Author

usagi commented Apr 6, 2012

result:

LH-MAIN /tmp% node hash_depth.js
{ time: 1045, title: 'direct' }
{ time: 1155, title: 'array depth-1' }
{ time: 1503, title: 'array depth-2' }
{ time: 1636, title: 'array depth-3' }
{ time: 1905, title: 'array depth-4' }
{ time: 2106, title: 'array depth-5' }

LH-MAIN /tmp% node --version
v0.6.13

LH-MAIN /tmp% uname -a
Linux LH-MAIN 3.2.11-1-ARCH #1 SMP PREEMPT Thu Mar 15 09:19:02 CET 2012 x86_64 AMD Phenom(tm) II X4 940 Processor AuthenticAMD GNU/Linux

@usagi
Copy link
Author

usagi commented Apr 6, 2012

@usagi
Copy link
Author

usagi commented Apr 6, 2012

{ time: 109, title: 'direct' }
{ time: 116, title: 'array depth-1' }
{ time: 144, title: 'array depth-2' }
{ time: 169, title: 'array depth-3' }
{ time: 191, title: 'array depth-4' }
{ time: 201, title: 'array depth-5' }
{ time: 1400, title: 'with depth-0(direct)' }
{ time: 1396, title: 'with depth-0(array depth-1)' }
{ time: 1401, title: 'with depth-0(array depth-2)' }
{ time: 1469, title: 'with depth-0(array depth-3)' }
{ time: 1405, title: 'with depth-0(array depth-4)' }
{ time: 1396, title: 'with depth-0(array depth-5)' }
{ time: 94, title: 'direct in function scope' }
{ time: 93, title: 'array depth-1 in function scope' }
{ time: 94, title: 'array depth-2 in function scope' }
{ time: 97, title: 'array depth-3 in function scope' }
{ time: 94, title: 'array depth-4 in function scope' }
{ time: 93, title: 'array depth-5 in function scope' }
{ time: 115, title: 'array depth-1(5) in function scope' }
{ time: 151, title: 'array depth-2(5) in function scope' }
{ time: 170, title: 'array depth-3(5) in function scope' }
{ time: 191, title: 'array depth-4(5) in function scope' }
{ time: 211, title: 'array depth-5(5) in function scope' }

@usagi
Copy link
Author

usagi commented Apr 6, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment