Initial commit
This commit is contained in:
commit
5eb60db655
7 changed files with 171 additions and 0 deletions
60
bench.js
Normal file
60
bench.js
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import {
|
||||
rangeGenerator,
|
||||
rangeObjectProperty,
|
||||
rangeClosure
|
||||
} from './range.js';
|
||||
|
||||
const thousand = 1000;
|
||||
const million = 1000000;
|
||||
|
||||
function benchmark(func) {
|
||||
const iters = 100 * thousand;
|
||||
// pre-heat jit
|
||||
for (let i = 0; i < iters; i++) {
|
||||
func();
|
||||
}
|
||||
|
||||
let start = performance.now();
|
||||
for (let i = 0; i < iters; i++) {
|
||||
func();
|
||||
}
|
||||
let delta = (performance.now() - start) / iters;
|
||||
console.log(`${delta * thousand} us per loop`);
|
||||
}
|
||||
|
||||
function testFor() {
|
||||
benchmark(() => {
|
||||
let acc = 0;
|
||||
for (let i = 0; i < thousand; i++) {
|
||||
acc += i;
|
||||
}
|
||||
return acc;
|
||||
});
|
||||
}
|
||||
|
||||
function testOne(func) {
|
||||
benchmark(() => {
|
||||
let acc = 0;
|
||||
for (let i of func(thousand)) {
|
||||
acc += i;
|
||||
}
|
||||
return acc;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
console.log('raw for loop');
|
||||
testFor();
|
||||
console.log('\n');
|
||||
|
||||
console.log('rangeGenerator');
|
||||
testOne(rangeGenerator);
|
||||
console.log('\n');
|
||||
|
||||
console.log('rangeObjectProperty');
|
||||
testOne(rangeObjectProperty);
|
||||
console.log('\n');
|
||||
|
||||
console.log('rangeClosure');
|
||||
testOne(rangeClosure);
|
||||
console.log('\n');
|
||||
Loading…
Add table
Add a link
Reference in a new issue