2016-02-20 02:03:16 +01:00
|
|
|
'use strict';
|
2017-09-14 03:48:53 +02:00
|
|
|
const common = require('../common.js');
|
2016-11-24 20:43:35 +01:00
|
|
|
|
2017-09-14 03:48:53 +02:00
|
|
|
const types = [
|
2016-11-24 20:43:35 +01:00
|
|
|
'Uint8',
|
|
|
|
'Uint16LE',
|
|
|
|
'Uint16BE',
|
|
|
|
'Uint32LE',
|
|
|
|
'Uint32BE',
|
|
|
|
'Int8',
|
|
|
|
'Int16LE',
|
|
|
|
'Int16BE',
|
|
|
|
'Int32LE',
|
|
|
|
'Int32BE',
|
|
|
|
'Float32LE',
|
|
|
|
'Float32BE',
|
|
|
|
'Float64LE',
|
|
|
|
'Float64BE'
|
|
|
|
];
|
|
|
|
|
2017-09-14 03:48:53 +02:00
|
|
|
const bench = common.createBenchmark(main, {
|
2016-11-24 20:43:35 +01:00
|
|
|
type: types,
|
2018-03-17 16:19:09 +01:00
|
|
|
n: [1e6]
|
2013-02-12 07:01:50 +01:00
|
|
|
});
|
|
|
|
|
2016-05-09 08:04:17 +02:00
|
|
|
const INT8 = 0x7f;
|
|
|
|
const INT16 = 0x7fff;
|
|
|
|
const INT32 = 0x7fffffff;
|
|
|
|
const UINT8 = INT8 * 2;
|
2013-02-12 07:01:50 +01:00
|
|
|
const UINT16 = INT16 * 2;
|
|
|
|
const UINT32 = INT32 * 2;
|
|
|
|
|
2017-09-14 03:48:53 +02:00
|
|
|
const mod = {
|
2013-02-12 07:01:50 +01:00
|
|
|
setInt8: INT8,
|
|
|
|
setInt16: INT16,
|
|
|
|
setInt32: INT32,
|
|
|
|
setUint8: UINT8,
|
|
|
|
setUint16: UINT16,
|
|
|
|
setUint32: UINT32
|
|
|
|
};
|
|
|
|
|
2018-03-17 16:19:09 +01:00
|
|
|
function main({ n, type }) {
|
2017-12-30 03:59:57 +01:00
|
|
|
type = type || 'Uint8';
|
2017-09-03 23:58:53 +02:00
|
|
|
const ab = new ArrayBuffer(8);
|
|
|
|
const dv = new DataView(ab, 0, 8);
|
|
|
|
const le = /LE$/.test(type);
|
|
|
|
const fn = `set${type.replace(/[LB]E$/, '')}`;
|
2013-02-12 07:01:50 +01:00
|
|
|
|
|
|
|
if (/int/i.test(fn))
|
2018-03-17 16:19:09 +01:00
|
|
|
benchInt(dv, fn, n, le);
|
2013-02-12 07:01:50 +01:00
|
|
|
else
|
2018-03-17 16:19:09 +01:00
|
|
|
benchFloat(dv, fn, n, le);
|
2013-02-12 07:01:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function benchInt(dv, fn, len, le) {
|
2016-05-22 22:06:42 +02:00
|
|
|
const m = mod[fn];
|
|
|
|
const method = dv[fn];
|
2013-02-12 07:01:50 +01:00
|
|
|
bench.start();
|
|
|
|
for (var i = 0; i < len; i++) {
|
2016-05-22 22:06:42 +02:00
|
|
|
method.call(dv, 0, i % m, le);
|
2013-02-12 07:01:50 +01:00
|
|
|
}
|
2018-03-17 16:19:09 +01:00
|
|
|
bench.end(len);
|
2013-02-12 07:01:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function benchFloat(dv, fn, len, le) {
|
2016-05-22 22:06:42 +02:00
|
|
|
const method = dv[fn];
|
2013-02-12 07:01:50 +01:00
|
|
|
bench.start();
|
|
|
|
for (var i = 0; i < len; i++) {
|
2016-05-22 22:06:42 +02:00
|
|
|
method.call(dv, 0, i * 0.1, le);
|
2013-02-12 07:01:50 +01:00
|
|
|
}
|
2018-03-17 16:19:09 +01:00
|
|
|
bench.end(len);
|
2013-02-12 07:01:50 +01:00
|
|
|
}
|