-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented and benchmarked array-map improvement (preallocating outp…
…ut array)
- Loading branch information
Showing
4 changed files
with
73 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const benchmark = require('benchmark'); | ||
const utils = require('./utils'); | ||
|
||
benchmark.options.onAbort = event => console.error(event.currentTarget.error); | ||
|
||
['small', 'medium', 'large'].forEach(arraySize => { | ||
const suite = new benchmark.Suite(); | ||
const title = `array-map-preallocation ${arraySize}`; | ||
|
||
suite.add('without preallocation', () => { | ||
const _defined = utils.randomArray(arraySize); | ||
const _defined2 = (e, i) => e + i; | ||
const results = []; | ||
for (let _i = 0; _i <= _defined.length - 1; _i++) { | ||
results.push(_defined2(_defined[_i], _i, _defined)); | ||
} | ||
}); | ||
suite.add('with preallocation', () => { | ||
const _defined = utils.randomArray(arraySize); | ||
const _defined2 = (e, i) => e + i; | ||
const results = new Array(_defined.length); | ||
for (let _i = 0; _i <= _defined.length - 1; _i++) { | ||
results[_i] = _defined2(_defined[_i], _i, _defined); | ||
} | ||
}); | ||
|
||
suite.on('start', () => console.log(' ' + title)); | ||
suite.on('cycle', event => console.log(" ✓ " + event.target)); | ||
suite.on('complete', function() { | ||
const slowest = this.filter('slowest')[0]; | ||
const fastest = this.filter('fastest')[0]; | ||
const microsecDelta = ((1000000 / slowest.hz) - (1000000 / fastest.hz)).toFixed(3); | ||
const percentFaster = (100 * (fastest.hz - slowest.hz) / slowest.hz).toFixed(1); | ||
console.log(fastest.name + ' is ' + percentFaster + '% faster (' + microsecDelta + 'μs) than ' + slowest.name + '\n'); | ||
}); | ||
|
||
suite.run(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a4854d2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implements #2
Results of running
node bench/array-map-preallocation.js
: