mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
4bb529d972
Apply strict mode to benchmark code. PR-URL: https://github.com/nodejs/node/pull/5336 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
65 lines
1.1 KiB
JavaScript
65 lines
1.1 KiB
JavaScript
'use strict';
|
|
var SlowBuffer = require('buffer').SlowBuffer;
|
|
var common = require('../common.js');
|
|
var assert = require('assert');
|
|
|
|
var bench = common.createBenchmark(main, {
|
|
size: [16, 512, 1024, 4096, 16386],
|
|
type: ['fast', 'slow'],
|
|
method: ['for', 'forOf', 'iterator'],
|
|
n: [1e3]
|
|
});
|
|
|
|
var methods = {
|
|
'for': benchFor,
|
|
'forOf': benchForOf,
|
|
'iterator': benchIterator
|
|
};
|
|
|
|
function main(conf) {
|
|
var len = +conf.size;
|
|
var clazz = conf.type === 'fast' ? Buffer : SlowBuffer;
|
|
var buffer = new clazz(len);
|
|
buffer.fill(0);
|
|
|
|
methods[conf.method](buffer, conf.n);
|
|
}
|
|
|
|
|
|
function benchFor(buffer, n) {
|
|
bench.start();
|
|
|
|
for (var k = 0; k < n; k++)
|
|
for (var i = 0; i < buffer.length; i++)
|
|
assert(buffer[i] === 0);
|
|
|
|
bench.end(n);
|
|
}
|
|
|
|
function benchForOf(buffer, n) {
|
|
bench.start();
|
|
|
|
for (var k = 0; k < n; k++)
|
|
for (var b of buffer)
|
|
assert(b === 0);
|
|
|
|
bench.end(n);
|
|
}
|
|
|
|
function benchIterator(buffer, n) {
|
|
bench.start();
|
|
|
|
for (var k = 0; k < n; k++) {
|
|
var iter = buffer[Symbol.iterator]();
|
|
var cur = iter.next();
|
|
|
|
while (!cur.done) {
|
|
assert(cur.value === 0);
|
|
cur = iter.next();
|
|
}
|
|
|
|
}
|
|
|
|
bench.end(n);
|
|
}
|