Skip to content

Instantly share code, notes, and snippets.

@zxcabs
Last active June 16, 2016 00:56
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 zxcabs/5d75c11f69445c4d9837 to your computer and use it in GitHub Desktop.
Save zxcabs/5d75c11f69445c4d9837 to your computer and use it in GitHub Desktop.
perfm
function index1(value) {
return (new Array(1,2,3)).indexOf(value);
}
function index2(value) {
return [1,2,3].indexOf(value);
}
function index3(value) {
function index(value) {
return (new Array(1,2,3)).indexOf(value);
}
return index(value);
}
function index4(value) {
function index(value) {
return [1,2,3].indexOf(value);
}
return index(value);
}
function index5(value) {
var arr = new Array(1,2,3);
function index(value) {
return arr.indexOf(value);
}
return index(value);
}
function index6(value) {
var arr = [1, 2, 3];
function index(value) {
return arr.indexOf(value);
}
return index(value);
}
function index7(value) {
var arr = new Array(1,2,3);
function index(arr, value) {
return arr.indexOf(value);
}
return index(arr, value);
}
function index8(value) {
var arr = [1,2,3];
function index(arr, value) {
return arr.indexOf(value);
}
return index(arr, value);
}
function test(fns) {
var i = 0,
fn,
name;
while (fn = fns[i]) {
name = fn.name + '_' + i;
console.time(name);
for (var j = 0; j < 5000000; j++) {
fn(2);
}
console.timeEnd(name);
i += 1;
}
}
test([index1, index2, index3, index4, index5, index6, index7, index8]);

$ node -v v4.3.1

  • index1_0: 110ms
  • index2_1: 48ms
  • index3_2: 523ms
  • index4_3: 10330ms
  • index5_4: 469ms
  • index6_5: 323ms
  • index7_6: 425ms
  • index8_7: 290ms
@mihasic
Copy link

mihasic commented Feb 24, 2016

Chrome 48.0.2564.116 m (64-bit)
index1_0: 139.270ms
index2_1: 57.373ms
index3_2: 721.241ms
index4_3: 930.062ms
index5_4: 688.851ms
index6_5: 494.212ms
index7_6: 596.103ms
index8_7: 455.884ms

@kraziant
Copy link

node -v 5.5.0
index1_0: 107.066ms
index2_1: 43.291ms
index3_2: 505.220ms
index4_3: 12536.065ms
index5_4: 449.090ms
index6_5: 305.469ms
index7_6: 405.998ms
index8_7: 285.871ms

@bisqet
Copy link

bisqet commented Jun 15, 2016

node -v v6.2.1
index1_0: 207.533ms
index2_1: 76.937ms
index3_2: 974.188ms
index4_3: 1283.026ms
index5_4: 835.188ms
index6_5: 626.149ms
index7_6: 751.602ms
index8_7: 551.675ms
тест с ноута, примерно делить на 2 можно^^

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