|
// npm i benny |
|
const b = require('benny'); |
|
|
|
function preallocCopy1 (src) { |
|
const res = new Array(src.length); |
|
for(var i=src.length; i-- > 0;) { |
|
res[i] = src[i]; |
|
} |
|
return res; |
|
} |
|
|
|
function preallocMap1 (f, src) { |
|
const res = new Array(src.length); |
|
for(var i=src.length; i-- > 0;) { |
|
res[i] = f(src[i]); |
|
} |
|
return res; |
|
} |
|
|
|
function preallocCopy2 (src) { |
|
const res = new Array(src.length); |
|
for(var i=src.length; i > 0; i--) { |
|
res[i] = src[i]; |
|
} |
|
return res; |
|
} |
|
|
|
function preallocMap2 (f, src) { |
|
const res = new Array(src.length); |
|
for(var i=src.length; i > 0; i--) { |
|
res[i] = f(src[i]); |
|
} |
|
return res; |
|
} |
|
|
|
function preallocCopy3 (src) { |
|
const len = src.length; |
|
const res = new Array(len); |
|
for(var i=0; i++ < len;) { |
|
res[i] = src[i]; |
|
} |
|
return res; |
|
} |
|
|
|
function preallocMap3 (f, src) { |
|
const len = src.length; |
|
const res = new Array(len); |
|
for(var i=0; i++ < len;) { |
|
res[i] = f(src[i]); |
|
} |
|
return res; |
|
} |
|
|
|
function preallocCopy4 (src) { |
|
const len = src.length; |
|
const res = new Array(len); |
|
for(var i=0; i < len; i++) { |
|
res[i] = src[i]; |
|
} |
|
return res; |
|
} |
|
|
|
function preallocMap4 (f, src) { |
|
const len = src.length; |
|
const res = new Array(len); |
|
for(var i=0; i < len; i++) { |
|
res[i] = f(src[i]); |
|
} |
|
return res; |
|
} |
|
|
|
const input = 'giyrxgfoyugrxeupqzuehfgfccsydgfpxwieuhqzuiehfuiegfyuegfpegrygrypiop634927chmiuqefdyiqdetfcityrfiwecfiuegciquegiyrxgfoyugrxeupqzuehfgfccsydgfpxwieuhqzuiehfuiegfyuegfpegrygrypiop634927chmiuqefdyiqdetfcityrfiwecfiuegciquegiyrxgfoyugrxeupqzuehfgfccsydgfpxwieuhqz'.split(''); |
|
|
|
b.suite( |
|
'map_for_etc', |
|
|
|
b.add('map(function ~ id)', () => { |
|
var output = input.map(function (x){ return x; }); |
|
}), |
|
|
|
b.add('map(arrow ~ id)', () => { |
|
var output = input.map((x) => x); |
|
}), |
|
|
|
b.add('map(function ~ push)', () => { |
|
var output = []; |
|
input.map(function (x){ output.push(x); }); |
|
}), |
|
|
|
b.add('map(arrow ~ push)', () => { |
|
var output = []; |
|
input.map((x) => output.push(x)); |
|
}), |
|
|
|
b.add('forEach(function ~ push)', () => { |
|
var output = []; |
|
input.forEach(function (x){ output.push(x); }); |
|
}), |
|
|
|
b.add('forEach(arrow ~ push)', () => { |
|
var output = []; |
|
input.forEach((x) => output.push(x)); |
|
}), |
|
|
|
b.add('for_of(push)', () => { |
|
var output = []; |
|
for(const x of input) { output.push(x); } |
|
}), |
|
|
|
b.add('for_i1(push ~ backwards, combined)', () => { |
|
var output = []; |
|
for(let i = input.length; i-- > 0;) { output.push(input[i]); } |
|
}), |
|
|
|
b.add('for_i2(push ~ backwards)', () => { |
|
var output = []; |
|
for(let i = input.length; i > 0; i--) { output.push(input[i]); } |
|
}), |
|
|
|
b.add('for_i3(push ~ forward, combined)', () => { |
|
const len = input.length; |
|
var output = []; |
|
for(let i = 0; i++ < len;) { output.push(input[i]); } |
|
}), |
|
|
|
b.add('for_i4(push ~ forward)', () => { |
|
const len = input.length; |
|
var output = []; |
|
for(let i = 0; i < len; i++) { output.push(input[i]); } |
|
}), |
|
|
|
b.add('for_i1(preallocate ~ backwards, combined)', () => { |
|
const len = input.length; |
|
var output = new Array(len); |
|
for(let i = len; i-- > 0;) { output[i] = input[i]; } |
|
}), |
|
|
|
b.add('for_i2(preallocate ~ backwards)', () => { |
|
const len = input.length; |
|
var output = new Array(len); |
|
for(let i = len; i > 0; i--) { output[i] = input[i]; } |
|
}), |
|
|
|
b.add('for_i3(preallocate ~ forward, combined)', () => { |
|
const len = input.length; |
|
var output = new Array(len); |
|
for(let i = 0; i++ < len;) { output[i] = input[i]; } |
|
}), |
|
|
|
b.add('for_i4(preallocate ~ forward)', () => { |
|
const len = input.length; |
|
var output = new Array(len); |
|
for(let i = 0; i < len; i++) { output[i] = input[i]; } |
|
}), |
|
|
|
b.add('preallocCopy1( ~ backwards, combined)', () => { |
|
var output = preallocCopy1(input); |
|
}), |
|
|
|
b.add('preallocCopy2( ~ backwards)', () => { |
|
var output = preallocCopy2(input); |
|
}), |
|
|
|
b.add('preallocCopy3( ~ forward, combined)', () => { |
|
var output = preallocCopy3(input); |
|
}), |
|
|
|
b.add('preallocCopy4( ~ forward)', () => { |
|
var output = preallocCopy4(input); |
|
}), |
|
|
|
b.add('preallocMap1(id ~ backwards, combined)', () => { |
|
var output = preallocMap1((x) => x, input); |
|
}), |
|
|
|
b.add('preallocMap2(id ~ backwards)', () => { |
|
var output = preallocMap2((x) => x, input); |
|
}), |
|
|
|
b.add('preallocMap3(id ~ forward, combined)', () => { |
|
var output = preallocMap3((x) => x, input); |
|
}), |
|
|
|
b.add('preallocMap4(id ~ forward)', () => { |
|
var output = preallocMap4((x) => x, input); |
|
}), |
|
|
|
b.cycle(), |
|
b.complete(), |
|
b.save({ file: 'map_for_etc', format: 'json' }), |
|
b.save({ file: 'map_for_etc', format: 'csv' }), |
|
b.save({ file: 'map_for_etc', format: 'chart.html' }), |
|
); |