Skip to content
Permalink
Browse files

buffer: add .from(), .alloc() and .allocUnsafe()

Several changes:

* Soft-Deprecate Buffer() constructors
* Add `Buffer.from()`, `Buffer.alloc()`, and `Buffer.allocUnsafe()`
* Add `--zero-fill-buffers` command line option
* Add byteOffset and length to `new Buffer(arrayBuffer)` constructor
* buffer.fill('') previously had no effect, now zero-fills
* Update the docs

PR-URL: #4682
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
  • Loading branch information...
jasnell committed Jan 25, 2016
1 parent 90a5fc2 commit 85ab4a5f1281c4e1dd06450ac7bd3250326267fa
Showing with 2,824 additions and 853 deletions.
  1. +1 −1 benchmark/buffers/buffer-base64-decode.js
  2. +1 −1 benchmark/buffers/buffer-base64-encode.js
  3. +1 −1 benchmark/buffers/buffer-bytelength.js
  4. +4 −4 benchmark/buffers/buffer-compare.js
  5. +50 −11 benchmark/buffers/buffer-creation.js
  6. +2 −2 benchmark/buffers/buffer-indexof.js
  7. +1 −1 benchmark/buffers/buffer-slice.js
  8. +1 −1 benchmark/buffers/buffer-tostring.js
  9. +2 −2 benchmark/buffers/buffer_zero.js
  10. +2 −2 benchmark/crypto/aes-gcm-throughput.js
  11. +1 −2 benchmark/crypto/cipher-stream.js
  12. +2 −3 benchmark/crypto/hash-stream-creation.js
  13. +1 −2 benchmark/crypto/hash-stream-throughput.js
  14. +1 −2 benchmark/crypto/rsa-encrypt-decrypt-throughput.js
  15. +1 −2 benchmark/crypto/rsa-sign-verify-throughput.js
  16. +1 −1 benchmark/dgram/array-vs-concat.js
  17. +1 −1 benchmark/dgram/multi-buffer.js
  18. +1 −1 benchmark/dgram/offset-length.js
  19. +1 −1 benchmark/dgram/single-buffer.js
  20. +1 −2 benchmark/fs-write-stream-throughput.js
  21. +1 −1 benchmark/fs/read-stream-throughput.js
  22. +1 −2 benchmark/fs/readfile.js
  23. +1 −2 benchmark/fs/write-stream-throughput.js
  24. +1 −1 benchmark/http/bench-parser.js
  25. +1 −2 benchmark/http/chunked.js
  26. +1 −2 benchmark/http/client-request-body.js
  27. +1 −2 benchmark/http/end-vs-write-end.js
  28. +1 −1 benchmark/http_simple.js
  29. +1 −1 benchmark/http_simple_auto.js
  30. +1 −2 benchmark/net/net-c2s-cork.js
  31. +1 −2 benchmark/net/net-c2s.js
  32. +1 −2 benchmark/net/net-pipe.js
  33. +1 −2 benchmark/net/net-s2c.js
  34. +1 −2 benchmark/net/tcp-raw-c2s.js
  35. +1 −2 benchmark/net/tcp-raw-pipe.js
  36. +1 −2 benchmark/net/tcp-raw-s2c.js
  37. +5 −5 benchmark/string_decoder/string-decoder.js
  38. +1 −3 benchmark/tls/throughput.js
  39. +439 −94 doc/api/buffer.markdown
  40. +3 −0 doc/node.1
  41. +1 −1 lib/_debugger.js
  42. +1 −1 lib/_http_client.js
  43. +1 −1 lib/_http_outgoing.js
  44. +2 −2 lib/_stream_readable.js
  45. +1 −1 lib/_stream_writable.js
  46. +1 −1 lib/_tls_legacy.js
  47. +2 −2 lib/_tls_wrap.js
  48. +1 −1 lib/assert.js
  49. +104 −34 lib/buffer.js
  50. +1 −1 lib/child_process.js
  51. +1 −1 lib/crypto.js
  52. +3 −3 lib/dgram.js
  53. +11 −11 lib/fs.js
  54. +1 −1 lib/internal/v8_prof_polyfill.js
  55. +1 −1 lib/net.js
  56. +1 −1 lib/querystring.js
  57. +1 −1 lib/string_decoder.js
  58. +2 −2 lib/tls.js
  59. +5 −5 lib/zlib.js
  60. +7 −1 src/node.cc
  61. +23 −4 src/node_buffer.cc
  62. +3 −0 src/node_buffer.h
  63. +1 −1 test/disabled/test-debug-brk-file.js
  64. +1 −2 test/disabled/test-dgram-send-error.js
  65. +2 −3 test/disabled/test-fs-largefile.js
  66. +1 −1 test/disabled/test-sendfd.js
  67. +1 −1 test/fixtures/GH-892-request.js
  68. +1 −1 test/fixtures/print-chars-from-buffer.js
  69. +4 −4 test/internet/test-dgram-broadcast-multi-process.js
  70. +4 −4 test/internet/test-dgram-multicast-multi-process.js
  71. +1 −1 test/internet/test-dgram-send-cb-quelches-error.js
  72. +1 −1 test/parallel/test-async-wrap-check-providers.js
  73. +1,442 −0 test/parallel/test-buffer-alloc.js
  74. +59 −3 test/parallel/test-buffer-arraybuffer.js
  75. +2 −2 test/parallel/test-buffer-ascii.js
  76. +2 −2 test/parallel/test-buffer-bytelength.js
  77. +4 −4 test/parallel/test-buffer-concat.js
  78. +3 −3 test/parallel/test-buffer-fakes.js
  79. +7 −7 test/parallel/test-buffer-fill.js
  80. +37 −35 test/parallel/test-buffer-includes.js
  81. +41 −41 test/parallel/test-buffer-indexof.js
  82. +4 −4 test/parallel/test-buffer-inspect.js
  83. +1 −1 test/parallel/test-buffer-iterator.js
  84. +14 −0 test/parallel/test-buffer-safe-unsafe.js
  85. +6 −5 test/parallel/test-buffer-slow.js
  86. +32 −0 test/parallel/test-buffer-zero-fill-cli.js
  87. +1 −1 test/parallel/test-buffer-zero-fill-reset.js
  88. +46 −43 test/parallel/test-buffer.js
  89. +1 −1 test/parallel/test-child-process-fork-dgram.js
  90. +4 −4 test/parallel/test-child-process-spawnsync-input.js
  91. +1 −1 test/parallel/test-child-process-spawnsync-maxbuf.js
  92. +2 −4 test/parallel/test-child-process-stdio-big-write-end.js
  93. +1 −1 test/parallel/test-cluster-dgram-1.js
  94. +1 −1 test/parallel/test-cluster-dgram-2.js
  95. +13 −13 test/parallel/test-crypto-authenticated.js
  96. +37 −37 test/parallel/test-crypto-binary-default.js
  97. +2 −2 test/parallel/test-crypto-cipher-decipher.js
  98. +4 −4 test/parallel/test-crypto-cipheriv-decipheriv.js
  99. +1 −2 test/parallel/test-crypto-dh-odd-key.js
  100. +2 −2 test/parallel/test-crypto-dh.js
  101. +1 −1 test/parallel/test-crypto-from-binary.js
  102. +2 −2 test/parallel/test-crypto-hash.js
  103. +30 −30 test/parallel/test-crypto-hmac.js
  104. +2 −2 test/parallel/test-crypto-padding-aes256.js
  105. +7 −7 test/parallel/test-crypto-rsa-dsa.js
  106. +3 −3 test/parallel/test-crypto-stream.js
  107. +1 −1 test/parallel/test-dgram-bytes-length.js
  108. +1 −2 test/parallel/test-dgram-close-is-not-callback.js
  109. +1 −2 test/parallel/test-dgram-close.js
  110. +4 −3 test/parallel/test-dgram-empty-packet.js
  111. +1 −1 test/parallel/test-dgram-exclusive-implicit-bind.js
  112. +2 −2 test/parallel/test-dgram-implicit-bind.js
  113. +1 −1 test/parallel/test-dgram-msgsize.js
  114. +1 −1 test/parallel/test-dgram-oob-buffer.js
  115. +1 −1 test/parallel/test-dgram-send-bad-arguments.js
  116. +1 −1 test/parallel/test-dgram-send-callback-buffer-length.js
  117. +1 −1 test/parallel/test-dgram-send-callback-buffer.js
  118. +2 −6 test/parallel/test-dgram-send-callback-multi-buffer.js
  119. +4 −5 test/parallel/test-dgram-send-default-host.js
  120. +1 −1 test/parallel/test-dgram-send-empty-buffer.js
  121. +2 −2 test/parallel/test-file-write-stream3.js
  122. +1 −1 test/parallel/test-fs-append-file-sync.js
  123. +1 −1 test/parallel/test-fs-append-file.js
  124. +1 −1 test/parallel/test-fs-read-buffer-tostring-fail.js
  125. +4 −4 test/parallel/test-fs-read-buffer-zero-length.js
  126. +4 −4 test/parallel/test-fs-read-buffer.js
  127. +1 −1 test/parallel/test-fs-read-stream-encoding.js
  128. +1 −1 test/parallel/test-fs-readfile-tostring-fail.js
  129. +1 −2 test/parallel/test-fs-readfile-unlink.js
  130. +1 −2 test/parallel/test-fs-sir-writes-alot.js
  131. +1 −2 test/parallel/test-fs-truncate.js
  132. +1 −2 test/parallel/test-fs-write-buffer.js
  133. +1 −1 test/parallel/test-fs-write-file-buffer.js
  134. +1 −1 test/parallel/test-fs-write-file.js
  135. +1 −1 test/parallel/test-fs-write-no-fd.js
  136. +1 −1 test/parallel/test-fs-write-stream-encoding.js
  137. +2 −2 test/parallel/test-fs-write-stream-err.js
  138. +1 −1 test/parallel/test-fs-write-sync.js
  139. +1 −1 test/parallel/test-http-buffer-sanity.js
  140. +1 −2 test/parallel/test-http-byteswritten.js
  141. +1 −1 test/parallel/test-http-client-upload-buf.js
  142. +1 −2 test/parallel/test-http-outgoing-finish.js
  143. +3 −3 test/parallel/test-http-parser-bad-ref.js
  144. +15 −15 test/parallel/test-http-parser.js
  145. +1 −1 test/parallel/test-http-pipeline-flood.js
  146. +1 −2 test/parallel/test-http-pipeline-regr-3332.js
  147. +1 −2 test/parallel/test-http-pipeline-regr-3508.js
  148. +1 −2 test/parallel/test-http-regr-gh-2821.js
  149. +2 −2 test/parallel/test-http-res-write-end-dont-take-array.js
  150. +2 −2 test/parallel/test-http-write-callbacks.js
  151. +1 −1 test/parallel/test-https-drain.js
  152. +3 −6 test/parallel/test-https-resume-after-renew.js
  153. +1 −1 test/parallel/test-https-truncate.js
  154. +2 −2 test/parallel/test-net-connect-buffer.js
  155. +1 −3 test/parallel/test-net-error-twice.js
  156. +1 −2 test/parallel/test-net-stream.js
  157. +1 −2 test/parallel/test-net-write-slow.js
  158. +2 −2 test/parallel/test-readline-interface.js
  159. +1 −1 test/parallel/test-regress-GH-io-1811.js
  160. +1 −2 test/parallel/test-stream-big-packet.js
  161. +1 −1 test/parallel/test-stream-pipe-cleanup-pause.js
  162. +3 −3 test/parallel/test-stream-readable-event.js
  163. +1 −1 test/parallel/test-stream-readable-flow-recursion.js
  164. +1 −1 test/parallel/test-stream-transform-constructor-set-methods.js
  165. +2 −2 test/parallel/test-stream-transform-split-objectmode.js
  166. +3 −5 test/parallel/test-stream-unshift-empty-chunk.js
  167. +4 −4 test/parallel/test-stream-unshift-read-race.js
  168. +3 −3 test/parallel/test-stream-writable-constructor-set-methods.js
  169. +1 −1 test/parallel/test-stream-writev.js
  170. +1 −1 test/parallel/test-stream2-base64-single-char-read-end.js
  171. +2 −3 test/parallel/test-stream2-compatibility.js
  172. +1 −1 test/parallel/test-stream2-finish-pipe.js
  173. +1 −1 test/parallel/test-stream2-large-read-stall.js
  174. +2 −2 test/parallel/test-stream2-pipe-error-handling.js
  175. +1 −1 test/parallel/test-stream2-read-sync-stack.js
  176. +8 −8 test/parallel/test-stream2-readable-empty-buffer-no-eof.js
  177. +4 −4 test/parallel/test-stream2-readable-from-list.js
  178. +1 −1 test/parallel/test-stream2-readable-legacy-drain.js
  179. +1 −1 test/parallel/test-stream2-readable-non-empty-end.js
  180. +2 −2 test/parallel/test-stream2-readable-wrap.js
  181. +1 −2 test/parallel/test-stream2-set-encoding.js
  182. +49 −50 test/parallel/test-stream2-transform.js
  183. +1 −1 test/parallel/test-stream2-unpipe-leak.js
  184. +12 −12 test/parallel/test-stream2-writable.js
  185. +1 −2 test/parallel/test-stream3-pause-then-read.js
  186. +2 −2 test/parallel/test-string-decoder-end.js
  187. +7 −8 test/parallel/test-string-decoder.js
  188. +2 −2 test/parallel/test-stringbytes-external-at-max.js
  189. +8 −8 test/parallel/test-stringbytes-external.js
  190. +1 −1 test/parallel/test-tls-alert-handling.js
  191. +1 −1 test/parallel/test-tls-cert-regression.js
  192. +1 −3 test/parallel/test-tls-client-destroy-soon.js
  193. +1 −2 test/parallel/test-tls-delayed-attach-error.js
  194. +1 −2 test/parallel/test-tls-fast-writing.js
  195. +1 −2 test/parallel/test-tls-hello-parser-failure.js
  196. +1 −1 test/parallel/test-tls-inception.js
  197. +1 −1 test/parallel/test-tls-max-send-fragment.js
  198. +1 −1 test/parallel/test-tls-ocsp-callback.js
  199. +1 −1 test/parallel/test-tls-pause.js
  200. +2 −2 test/parallel/test-tty-wrap.js
  201. +1 −1 test/parallel/test-util.js
  202. +3 −3 test/parallel/test-zlib-dictionary-fail.js
  203. +1 −1 test/parallel/test-zlib-dictionary.js
  204. +1 −1 test/parallel/test-zlib-flush-drain.js
  205. +3 −3 test/parallel/test-zlib-flush.js
  206. +2 −2 test/parallel/test-zlib-from-string.js
  207. +1 −1 test/parallel/test-zlib-params.js
  208. +1 −1 test/parallel/test-zlib-random-byte-pipes.js
  209. +1 −1 test/parallel/test-zlib-zero-byte.js
  210. +1 −1 test/parallel/test-zlib.js
  211. +1 −1 test/pummel/test-https-no-reader.js
  212. +2 −2 test/pummel/test-regress-GH-814.js
  213. +2 −2 test/pummel/test-regress-GH-814_2.js
  214. +1 −1 test/pummel/test-stream-pipe-multi.js
  215. +3 −4 test/pummel/test-stream2-basic.js
  216. +1 −1 test/pummel/test-tls-server-large-request.js
  217. +3 −3 test/sequential/test-buffer-bad-overload.js
  218. +1 −1 test/sequential/test-child-process-execsync.js
  219. +1 −3 test/sequential/test-net-GH-5504.js
  220. +1 −2 test/sequential/test-pipe.js
  221. +2 −2 test/sequential/test-regress-GH-1697.js
  222. +2 −2 test/sequential/test-stringbytes-external-exceed-max-by-1-ascii.js
  223. +2 −2 test/sequential/test-stringbytes-external-exceed-max-by-1-base64.js
  224. +2 −2 test/sequential/test-stringbytes-external-exceed-max-by-1-binary.js
  225. +2 −2 test/sequential/test-stringbytes-external-exceed-max-by-1-hex.js
  226. +2 −2 test/sequential/test-stringbytes-external-exceed-max-by-1-utf8.js
  227. +2 −2 test/sequential/test-stringbytes-external-exceed-max-by-2.js
  228. +2 −2 test/sequential/test-stringbytes-external-exceed-max.js
  229. +1 −1 test/sequential/test-zerolengthbufferbug.js
@@ -8,7 +8,7 @@ function main(conf) {
const s = 'abcd'.repeat(8 << 20);
s.match(/./); // Flatten string.
assert.equal(s.length % 4, 0);
const b = Buffer(s.length / 4 * 3);
const b = Buffer.allocUnsafe(s.length / 4 * 3);
b.write(s, 0, s.length, 'base64');
bench.start();
for (var i = 0; i < 32; i += 1) b.base64Write(s, 0, s.length);
@@ -5,7 +5,7 @@ var bench = common.createBenchmark(main, {});

function main(conf) {
var N = 64 * 1024 * 1024;
var b = Buffer(N);
var b = Buffer.allocUnsafe(N);
var s = '';
var i;
for (i = 0; i < 256; ++i) s += String.fromCharCode(i);
@@ -28,7 +28,7 @@ function main(conf) {
strings.push(data);
} else if (encoding === 'base64') {
// Base64 strings will be much longer than their UTF8 counterparts
strings.push(new Buffer(data, 'utf8').toString('base64'));
strings.push(Buffer.from(data, 'utf8').toString('base64'));
}
}

@@ -7,10 +7,10 @@ var bench = common.createBenchmark(main, {
});

function main(conf) {
var iter = (conf.millions >>> 0) * 1e6;
var size = (conf.size >>> 0);
var b0 = new Buffer(size).fill('a');
var b1 = new Buffer(size).fill('a');
const iter = (conf.millions >>> 0) * 1e6;
const size = (conf.size >>> 0);
const b0 = Buffer.alloc(size, 'a');
const b1 = Buffer.alloc(size, 'a');

b1[size - 1] = 'b'.charCodeAt(0);

@@ -1,20 +1,59 @@
'use strict';
const SlowBuffer = require('buffer').SlowBuffer;

var common = require('../common.js');
var bench = common.createBenchmark(main, {
type: ['fast', 'slow'],
len: [10, 1024],
const common = require('../common.js');
const assert = require('assert');
const bench = common.createBenchmark(main, {
type: [
'fast-alloc',
'fast-alloc-fill',
'fast-allocUnsafe',
'slow',
'buffer()'],
len: [10, 1024, 2048, 4096, 8192],
n: [1024]
});

function main(conf) {
var len = +conf.len;
var n = +conf.n;
var clazz = conf.type === 'fast' ? Buffer : SlowBuffer;
bench.start();
for (var i = 0; i < n * 1024; i++) {
new clazz(len);
const len = +conf.len;
const n = +conf.n;
switch (conf.type) {
case 'fast-alloc':
bench.start();
for (let i = 0; i < n * 1024; i++) {
Buffer.alloc(len);
}
bench.end(n);
break;
case 'fast-alloc-fill':
bench.start();
for (let i = 0; i < n * 1024; i++) {
Buffer.alloc(len, 0);
}
bench.end(n);
break;
case 'fast-allocUnsafe':
bench.start();
for (let i = 0; i < n * 1024; i++) {
Buffer.allocUnsafe(len);
}
bench.end(n);
break;
case 'slow':
bench.start();
for (let i = 0; i < n * 1024; i++) {
SlowBuffer(len);
}
bench.end(n);
break;
case 'buffer()':
bench.start();
for (let i = 0; i < n * 1024; i++) {
Buffer(len);
}
bench.end(n);
break;
default:
assert.fail(null, null, 'Should not get here');
}
bench.end(n);
}
@@ -27,11 +27,11 @@ function main(conf) {
}

if (encoding === 'ucs2') {
aliceBuffer = new Buffer(aliceBuffer.toString(), encoding);
aliceBuffer = Buffer.from(aliceBuffer.toString(), encoding);
}

if (conf.type === 'buffer') {
search = new Buffer(new Buffer(search).toString(), encoding);
search = Buffer.from(Buffer.from(search).toString(), encoding);
}

bench.start();
@@ -7,7 +7,7 @@ var bench = common.createBenchmark(main, {
n: [1024]
});

var buf = new Buffer(1024);
var buf = Buffer.allocUnsafe(1024);
var slowBuf = new SlowBuffer(1024);

function main(conf) {
@@ -12,7 +12,7 @@ function main(conf) {
const arg = conf.arg === 'true';
const len = conf.len | 0;
const n = conf.n | 0;
const buf = Buffer(len).fill(42);
const buf = Buffer.alloc(len, 42);

var i;
bench.start();
@@ -6,13 +6,13 @@ const bench = common.createBenchmark(main, {
n: [1024]
});

const zero = new Buffer(0);
const zero = Buffer.alloc(0);

function main(conf) {
var n = +conf.n;
bench.start();
for (let i = 0; i < n * 1024; i++) {
new Buffer(zero);
Buffer.from(zero);
}
bench.end(n);
}
@@ -9,10 +9,10 @@ var bench = common.createBenchmark(main, {
});

function main(conf) {
var message = (new Buffer(conf.len)).fill('b');
var message = Buffer.alloc(conf.len, 'b');
var key = crypto.randomBytes(keylen[conf.cipher]);
var iv = crypto.randomBytes(12);
var associate_data = (new Buffer(16)).fill('z');
var associate_data = Buffer.alloc(16, 'z');
bench.start();
AEAD_Bench(conf.cipher, message, associate_data, key, iv, conf.n, conf.len);
}
@@ -48,8 +48,7 @@ function main(conf) {
encoding = 'utf8';
break;
case 'buf':
message = new Buffer(conf.len);
message.fill('b');
message = Buffer.alloc(conf.len, 'b');
break;
default:
throw new Error('unknown message type: ' + conf.type);
@@ -33,8 +33,7 @@ function main(conf) {
encoding = 'utf8';
break;
case 'buf':
message = new Buffer(conf.len);
message.fill('b');
message = Buffer.alloc(conf.len, 'b');
break;
default:
throw new Error('unknown message type: ' + conf.type);
@@ -58,7 +57,7 @@ function legacyWrite(algo, message, encoding, writes, len, outEnc) {

// include buffer creation costs for older versions
if (outEnc === 'buffer' && typeof res === 'string')
res = new Buffer(res, 'binary');
res = Buffer.from(res, 'binary');
}

bench.end(gbits);
@@ -32,8 +32,7 @@ function main(conf) {
encoding = 'utf8';
break;
case 'buf':
message = new Buffer(conf.len);
message.fill('b');
message = Buffer.alloc(conf.len, 'b');
break;
default:
throw new Error('unknown message type: ' + conf.type);
@@ -23,8 +23,7 @@ var bench = common.createBenchmark(main, {
});

function main(conf) {
var message = (new Buffer(conf.len)).fill('b');

var message = Buffer.alloc(conf.len, 'b');
bench.start();
StreamWrite(conf.algo, conf.keylen, message, conf.n, conf.len);
}
@@ -24,8 +24,7 @@ var bench = common.createBenchmark(main, {
});

function main(conf) {
var message = (new Buffer(conf.len)).fill('b');

var message = Buffer.alloc(conf.len, 'b');
bench.start();
StreamWrite(conf.algo, conf.keylen, message, conf.writes, conf.len);
}
@@ -31,7 +31,7 @@ function main(conf) {

chunk = [];
for (var i = 0; i < chunks; i++) {
chunk.push(new Buffer(Math.round(len / chunks)));
chunk.push(Buffer.allocUnsafe(Math.round(len / chunks)));
}

server();
@@ -31,7 +31,7 @@ function main(conf) {

chunk = [];
for (var i = 0; i < chunks; i++) {
chunk.push(new Buffer(Math.round(len / chunks)));
chunk.push(Buffer.allocUnsafe(Math.round(len / chunks)));
}

server();
@@ -25,7 +25,7 @@ function main(conf) {
len = +conf.len;
num = +conf.num;
type = conf.type;
chunk = new Buffer(len);
chunk = Buffer.allocUnsafe(len);
server();
}

@@ -25,7 +25,7 @@ function main(conf) {
len = +conf.len;
num = +conf.num;
type = conf.type;
chunk = new Buffer(len);
chunk = Buffer.allocUnsafe(len);
server();
}

@@ -45,8 +45,7 @@ function runTest(dur, size, type) {
chunk = new Array(size + 1).join('a');
break;
case 'buffer':
chunk = new Buffer(size);
chunk.fill('a');
chunk = Buffer.alloc(size, 'a');
break;
}

@@ -60,7 +60,7 @@ function runTest() {
}

function makeFile() {
var buf = new Buffer(filesize / 1024);
var buf = Buffer.allocUnsafe(filesize / 1024);
if (encoding === 'utf8') {
// ü
for (var i = 0; i < buf.length; i++) {
@@ -17,8 +17,7 @@ var bench = common.createBenchmark(main, {
function main(conf) {
var len = +conf.len;
try { fs.unlinkSync(filename); } catch (e) {}
var data = new Buffer(len);
data.fill('x');
var data = Buffer.alloc(len, 'x');
fs.writeFileSync(filename, data);
data = null;

@@ -21,8 +21,7 @@ function main(conf) {
var chunk;
switch (type) {
case 'buf':
chunk = new Buffer(size);
chunk.fill('b');
chunk = Buffer.alloc(size, 'b');
break;
case 'asc':
chunk = new Array(size + 1).join('a');
@@ -25,7 +25,7 @@ function main(conf) {
}
header += CRLF;

processHeader(new Buffer(header), n);
processHeader(Buffer.from(header), n);
}


@@ -18,8 +18,7 @@ var bench = common.createBenchmark(main, {

function main(conf) {
const http = require('http');
var chunk = new Buffer(conf.size);
chunk.fill('8');
var chunk = Buffer.alloc(conf.size, '8');

var args = ['-d', '10s', '-t', 8, '-c', conf.c];

@@ -19,8 +19,7 @@ function main(conf) {
var chunk;
switch (conf.type) {
case 'buf':
chunk = new Buffer(len);
chunk.fill('x');
chunk = Buffer.alloc(len, 'x');
break;
case 'utf':
encoding = 'utf8';
@@ -23,8 +23,7 @@ function main(conf) {
var len = conf.kb * 1024;
switch (conf.type) {
case 'buf':
chunk = new Buffer(len);
chunk.fill('x');
chunk = Buffer.alloc(len, 'x');
break;
case 'utf':
chunk = new Array(len / 2 + 1).join('ü');
@@ -51,7 +51,7 @@ var server = module.exports = http.createServer(function(req, res) {
if (n <= 0)
throw new Error('buffer called with n <= 0');
if (storedBuffer[n] === undefined) {
storedBuffer[n] = new Buffer(n);
storedBuffer[n] = Buffer.allocUnsafe(n);
for (i = 0; i < n; i++) {
storedBuffer[n][i] = 'C'.charCodeAt(0);
}
@@ -47,7 +47,7 @@ var server = http.createServer(function(req, res) {
n = parseInt(arg, 10);
if (n <= 0) throw new Error('bytes called with n <= 0');
if (storedBuffer[n] === undefined) {
storedBuffer[n] = new Buffer(n);
storedBuffer[n] = Buffer.allocUnsafe(n);
for (i = 0; i < n; i++) {
storedBuffer[n][i] = 'C'.charCodeAt(0);
}
@@ -23,8 +23,7 @@ function main(conf) {

switch (type) {
case 'buf':
chunk = new Buffer(len);
chunk.fill('x');
chunk = Buffer.alloc(len, 'x');
break;
case 'utf':
encoding = 'utf8';
@@ -23,8 +23,7 @@ function main(conf) {

switch (type) {
case 'buf':
chunk = new Buffer(len);
chunk.fill('x');
chunk = Buffer.alloc(len, 'x');
break;
case 'utf':
encoding = 'utf8';
@@ -23,8 +23,7 @@ function main(conf) {

switch (type) {
case 'buf':
chunk = new Buffer(len);
chunk.fill('x');
chunk = Buffer.alloc(len, 'x');
break;
case 'utf':
encoding = 'utf8';

0 comments on commit 85ab4a5

Please sign in to comment.
You can’t perform that action at this time.