Why running benchmark tests in the console gives different results
I wanted to test the effectiveness of various ways of checking if a value is an array. I've used console.time and jsperf and got conflicting results. Generating the script and running it gave similar jsperf results.
jsperf link http://jsperf.com/is-it-an-array
my code
a={test:'a',foo:'bar'};
b='abcdef';
c=[1,2,3,4,5,6]
d=3.14
length=1e6;
l=length;
console.time(1);
while(l--){
a instanceof Array ?true:false;
b instanceof Array ?true:false;
c instanceof Array ?true:false;
d instanceof Array ?true:false;
}
console.timeEnd(1);
l=length;
console.time(2);
while(l--){
a.pop?true:false;
b.pop?true:false;
c.pop?true:false;
d.pop?true:false;
}
console.timeEnd(2);
l=length;
console.time(3);
while(l--){
Array.isArray(a)?true:false;
Array.isArray(b)?true:false;
Array.isArray(c)?true:false;
Array.isArray(d)?true:false;
}
console.timeEnd(3);
l=length;
console.time(4);
while(l--){
a.constructor == Array?true:false;
b.constructor == Array?true:false;
c.constructor == Array?true:false;
d.constructor == Array?true:false;
}
console.timeEnd(4);
On my computer Firefox works better with them than Chrome using jsperf. The fastest solution is to use instanceof
and the worst is .pop
. In chrome it isArray
is the fastest and .pop
still the slowest.
But running these tests in the console .pop
will never get slower.
Firefox
1: 153.35ms
2: 166.88ms //.pop
3: 169.94ms
4: 193.73ms
Chromium
1: 286.000ms
2: 207.000ms //.pop
3: 270.000ms
4: 266.000ms
What does using the console to run code give different results?
source to share
No one has answered this question yet
Check out similar questions: