Skip to content

Instantly share code, notes, and snippets.

@JacksonTian
Last active August 29, 2015 14:19
Show Gist options
  • Save JacksonTian/2c9e2bdec00018e010e6 to your computer and use it in GitHub Desktop.
Save JacksonTian/2c9e2bdec00018e010e6 to your computer and use it in GitHub Desktop.
Buffer.concat
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite();
var buff = new Buffer(10);
var buff100 = new Buffer(100);
var buff1000 = new Buffer(1000);
var buff8000 = new Buffer(8000);
var make_buffers = function (buf, length) {
var list = [];
for (var i = 0; i < length; i++) {
list.push(buf);
}
return list;
};
var new_concat = function(list, length) {
if (!Array.isArray(list))
throw new TypeError('list argument must be an Array of Buffers.');
if (list.length === 0)
return new Buffer(0);
else if (list.length === 1)
return list[0];
if (length === undefined) {
length = 0;
for (var i = 0; i < list.length; i++)
length += list[i].length;
} else {
length = length >>> 0;
}
var buffer = new Buffer(length);
var pos = 0;
for (var i = 0; i < list.length; i++) {
var buf = list[i];
buf.copy(buffer, pos);
pos += buf.length;
}
return buffer;
};
var old_concat = function(list, length) {
if (!Array.isArray(list))
throw new TypeError('list argument must be an Array of Buffers.');
if (length === undefined) {
length = 0;
for (var i = 0; i < list.length; i++)
length += list[i].length;
} else {
length = length >>> 0;
}
if (list.length === 0)
return new Buffer(0);
else if (list.length === 1)
return list[0];
var buffer = new Buffer(length);
var pos = 0;
for (var i = 0; i < list.length; i++) {
var buf = list[i];
buf.copy(buffer, pos);
pos += buf.length;
}
return buffer;
};
var empty_list = make_buffers(new Buffer(0), 0);
var buff_1 = make_buffers(buff, 1);
var buff_2 = make_buffers(buff, 2);
var buff_10 = make_buffers(buff, 10);
var buff_100 = make_buffers(buff, 100);
var buff100_1 = make_buffers(buff100, 1);
var buff100_2 = make_buffers(buff100, 2);
var buff100_10 = make_buffers(buff100, 10);
var buff100_100 = make_buffers(buff100, 100);
var buff1000_1 = make_buffers(buff1000, 1);
var buff1000_2 = make_buffers(buff1000, 2);
var buff1000_10 = make_buffers(buff1000, 10);
var buff1000_100 = make_buffers(buff1000, 100);
suite
.add("new length 0", function () {
new_concat(empty_list);
})
.add('new length 1', function () {
new_concat(buff_1);
})
.add("new length 2", function () {
new_concat(buff_2);
})
.add("new length 10", function () {
new_concat(buff_10);
})
.add("new length 100", function () {
new_concat(buff_100);
})
.add("old length 0", function () {
old_concat(empty_list);
})
.add('old length 1', function () {
old_concat(buff_1);
})
.add("old length 2", function () {
old_concat(buff_2);
})
.add("old length 10", function () {
old_concat(buff_10);
})
.add("old length 100", function () {
old_concat(buff_100);
})
.add("buff100 new length 0", function () {
new_concat(empty_list);
})
.add('buff100 new length 1', function () {
new_concat(buff100_1);
})
.add("buff100 new length 2", function () {
new_concat(buff100_2);
})
.add("buff100 new length 10", function () {
new_concat(buff100_10);
})
.add("buff100 new length 100", function () {
new_concat(buff100_100);
})
.add("buff100 old length 0", function () {
old_concat(empty_list);
})
.add('buff100 old length 1', function () {
old_concat(buff100_1);
})
.add("buff100 old length 2", function () {
old_concat(buff100_2);
})
.add("buff100 old length 10", function () {
old_concat(buff100_10);
})
.add("buff100 old length 100", function () {
old_concat(buff100_100);
})
.add("buff1000 new length 0", function () {
new_concat(empty_list);
})
.add('buff1000 new length 1', function () {
new_concat(buff1000_1);
})
.add("buff1000 new length 2", function () {
new_concat(buff1000_2);
})
.add("buff1000 new length 10", function () {
new_concat(buff1000_10);
})
.add("buff1000 new length 100", function () {
new_concat(buff1000_100);
})
.add("buff1000 old length 0", function () {
old_concat(empty_list);
})
.add('buff1000 old length 1', function () {
old_concat(buff1000_1);
})
.add("buff1000 old length 2", function () {
old_concat(buff1000_2);
})
.add("buff1000 old length 10", function () {
old_concat(buff1000_10);
})
.add("buff1000 old length 100", function () {
old_concat(buff1000_100);
})
// add listeners
.on('cycle', function (event) {
console.log(String(event.target));
})
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})
.run({ async: false });
new length 0 x 1,034,624 ops/sec ±10.05% (43 runs sampled)
new length 1 x 45,085,806 ops/sec ±1.91% (84 runs sampled)
new length 2 x 2,051,267 ops/sec ±3.48% (83 runs sampled)
new length 10 x 821,860 ops/sec ±2.83% (81 runs sampled)
new length 100 x 110,519 ops/sec ±3.61% (82 runs sampled)
old length 0 x 1,127,066 ops/sec ±12.44% (48 runs sampled)
old length 1 x 33,351,696 ops/sec ±3.21% (83 runs sampled)
old length 2 x 1,862,283 ops/sec ±2.60% (80 runs sampled)
old length 10 x 757,834 ops/sec ±2.87% (79 runs sampled)
old length 100 x 101,986 ops/sec ±4.56% (78 runs sampled)
buff100 new length 0 x 1,023,448 ops/sec ±13.61% (49 runs sampled)
buff100 new length 1 x 30,414,166 ops/sec ±2.36% (78 runs sampled)
buff100 new length 2 x 1,567,395 ops/sec ±2.90% (77 runs sampled)
buff100 new length 10 x 480,774 ops/sec ±8.99% (66 runs sampled)
buff100 new length 100 x 62,559 ops/sec ±9.34% (67 runs sampled)
buff100 old length 0 x 1,053,923 ops/sec ±12.64% (46 runs sampled)
buff100 old length 1 x 26,407,231 ops/sec ±2.50% (81 runs sampled)
buff100 old length 2 x 1,507,457 ops/sec ±3.31% (78 runs sampled)
buff100 old length 10 x 476,436 ops/sec ±9.79% (63 runs sampled)
buff100 old length 100 x 62,212 ops/sec ±9.47% (66 runs sampled)
buff1000 new length 0 x 1,080,568 ops/sec ±13.01% (48 runs sampled)
buff1000 new length 1 x 29,699,749 ops/sec ±2.22% (77 runs sampled)
buff1000 new length 2 x 455,777 ops/sec ±9.82% (55 runs sampled)
buff1000 new length 10 x 116,589 ops/sec ±8.86% (56 runs sampled)
buff1000 new length 100 x 15,547 ops/sec ±4.71% (52 runs sampled)
buff1000 old length 0 x 1,102,854 ops/sec ±12.80% (49 runs sampled)
buff1000 old length 1 x 29,228,265 ops/sec ±2.07% (85 runs sampled)
buff1000 old length 2 x 455,276 ops/sec ±9.88% (53 runs sampled)
buff1000 old length 10 x 115,662 ops/sec ±8.60% (55 runs sampled)
buff1000 old length 100 x 15,664 ops/sec ±1.99% (44 runs sampled)
Fastest is new length 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment