Skip to content

Instantly share code, notes, and snippets.

@nagisa
Created August 25, 2011 06:59
Show Gist options
  • Save nagisa/1170125 to your computer and use it in GitHub Desktop.
Save nagisa/1170125 to your computer and use it in GitHub Desktop.
Benchmarking JavaScript
start = new Date();
for(i=0; i<1000000; i++){String(Math.floor(Math.random()*1000000))};
console.log(new Date()-start)
>> 2054
start = new Date();
for(i=0; i<1000000; i++){String(~~(Math.random()*1000000))};
console.log(new Date()-start)
>> 1752
start = new Date();
for(i=0; i<1000000; i++){~~(Math.random()*1000000)};
console.log(new Date()-start)
>> 1218
#Testing array subscription
array = [];
for(i=0; i<10000; i++){array.push(i)};
start = new Date();
for(i=0; i<100000; i++){array['5000']};
console.log(new Date() - start);
>> 140
start = new Date();
for(i=0; i<100000; i++){array[5000]};
console.log(new Date() - start);
>> 125
start = new Date();
for(i=0; i<100000; i++){array[String(~~(Math.random()*1000000))]};
console.log(new Date() - start);
>> 317
start = new Date();
for(i=0; i<100000; i++){array[~~(Math.random()*1000000)]};
console.log(new Date() - start);
>> 239
random = ~~(Math.random()*1000000); randoms = String(random); array[random] === array[randoms]
>> true
object_ = {1:'a', '2': 4};
object_['1'] == object_[1]
>> true
object_['2'] == object_[2]
>> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment