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: https://github.com/nodejs/node/pull/4682
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
This commit is contained in:
James M Snell
2016-01-25 15:00:06 -08:00
parent 90a5fc20be
commit 85ab4a5f12
229 changed files with 2825 additions and 854 deletions

View File

@@ -9,7 +9,7 @@ var bench = common.createBenchmark(main, {
n: [25e4]
});
var UTF_ALPHA = 'Blåbærsyltetøy';
var UTF_ALPHA = 'Blåbærsyltetøy';
var ASC_ALPHA = 'Blueberry jam';
function main(conf) {
@@ -35,18 +35,18 @@ function main(conf) {
for (i = 0; i < inLen; ++i) {
if (i > 0 && (i % chunkLen) === 0 && !isBase64) {
chunks.push(new Buffer(str, encoding));
chunks.push(Buffer.from(str, encoding));
str = '';
}
str += alpha[i % alpha.length];
}
if (str.length > 0 && !isBase64)
chunks.push(new Buffer(str, encoding));
chunks.push(Buffer.from(str, encoding));
if (isBase64) {
str = new Buffer(str, 'utf8').toString('base64');
str = Buffer.from(str, 'utf8').toString('base64');
while (str.length > 0) {
var len = Math.min(chunkLen, str.length);
chunks.push(new Buffer(str.substring(0, len), 'utf8'));
chunks.push(Buffer.from(str.substring(0, len), 'utf8'));
str = str.substring(len);
}
}