2016-02-20 02:03:16 +01:00
|
|
|
'use strict';
|
2015-05-16 04:24:34 +02:00
|
|
|
var common = require('../common');
|
|
|
|
|
|
|
|
var bench = common.createBenchmark(main, {
|
2017-04-10 11:03:57 +02:00
|
|
|
encoding: ['utf8', 'base64', 'buffer'],
|
2015-05-16 04:24:34 +02:00
|
|
|
len: [1, 2, 4, 16, 64, 256], // x16
|
|
|
|
n: [5e6]
|
|
|
|
});
|
|
|
|
|
|
|
|
// 16 chars each
|
|
|
|
var chars = [
|
|
|
|
'hello brendan!!!', // 1 byte
|
|
|
|
'ΰαβγδεζηθικλμνξο', // 2 bytes
|
|
|
|
'挰挱挲挳挴挵挶挷挸挹挺挻挼挽挾挿', // 3 bytes
|
|
|
|
'𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎𠸏𠹷𠺝𠺢' // 4 bytes
|
|
|
|
];
|
|
|
|
|
|
|
|
function main(conf) {
|
|
|
|
var n = conf.n | 0;
|
|
|
|
var len = conf.len | 0;
|
|
|
|
var encoding = conf.encoding;
|
|
|
|
|
|
|
|
var strings = [];
|
2017-04-10 11:03:57 +02:00
|
|
|
var results;
|
|
|
|
if (encoding === 'buffer') {
|
|
|
|
strings = [ Buffer.alloc(len * 16, 'a') ];
|
|
|
|
results = [ len * 16 ];
|
|
|
|
} else {
|
|
|
|
for (var string of chars) {
|
|
|
|
// Strings must be built differently, depending on encoding
|
2017-04-17 03:01:12 +02:00
|
|
|
var data = string.repeat(len);
|
2017-04-10 11:03:57 +02:00
|
|
|
if (encoding === 'utf8') {
|
|
|
|
strings.push(data);
|
|
|
|
} else if (encoding === 'base64') {
|
|
|
|
// Base64 strings will be much longer than their UTF8 counterparts
|
|
|
|
strings.push(Buffer.from(data, 'utf8').toString('base64'));
|
|
|
|
}
|
2015-05-16 04:24:34 +02:00
|
|
|
}
|
|
|
|
|
2017-04-10 11:03:57 +02:00
|
|
|
// Check the result to ensure it is *properly* optimized
|
|
|
|
results = strings.map(function(val) {
|
|
|
|
return Buffer.byteLength(val, encoding);
|
|
|
|
});
|
|
|
|
}
|
2015-05-16 04:24:34 +02:00
|
|
|
|
|
|
|
bench.start();
|
|
|
|
for (var i = 0; i < n; i++) {
|
|
|
|
var index = n % strings.length;
|
|
|
|
// Go!
|
|
|
|
var r = Buffer.byteLength(strings[index], encoding);
|
|
|
|
|
|
|
|
if (r !== results[index])
|
2016-02-25 07:30:10 +01:00
|
|
|
throw new Error('incorrect return value');
|
2015-05-16 04:24:34 +02:00
|
|
|
}
|
|
|
|
bench.end(n);
|
|
|
|
}
|