diff --git a/benchmark/buffers/buffer_zero.js b/benchmark/buffers/buffer_zero.js new file mode 100644 index 00000000000000..461378758b5951 --- /dev/null +++ b/benchmark/buffers/buffer_zero.js @@ -0,0 +1,18 @@ +'use strict'; + +const common = require('../common.js'); + +const bench = common.createBenchmark(main, { + n: [1024] +}); + +const zero = new Buffer(0); + +function main(conf) { + var n = +conf.n; + bench.start(); + for (let i = 0; i < n * 1024; i++) { + new Buffer(zero); + } + bench.end(n); +} diff --git a/lib/buffer.js b/lib/buffer.js index 5b334ec5a50adb..2daebb5e301e1c 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -121,6 +121,10 @@ function fromString(string, encoding) { function fromObject(obj) { if (obj instanceof Buffer) { var b = allocate(obj.length); + + if (b.length === 0) + return b; + obj.copy(b, 0, 0, obj.length); return b; }