-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
buffer: implement
iterable
interface
This makes possible to use `for..of` loop with buffers. Also related `keys`, `values` and `entries` methods are added for feature parity with `Uint8Array`. PR-URL: #525 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
- Loading branch information
1 parent
3cbb5cd
commit 45d8d9f
Showing
4 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
var SlowBuffer = require('buffer').SlowBuffer; | ||
var common = require('../common.js'); | ||
var assert = require('assert'); | ||
|
||
var bench = common.createBenchmark(main, { | ||
size: [16, 512, 1024, 4096, 16386], | ||
type: ['fast', 'slow'], | ||
method: ['for', 'forOf', 'iterator'], | ||
n: [1e3] | ||
}); | ||
|
||
var methods = { | ||
'for': benchFor, | ||
'forOf': benchForOf, | ||
'iterator': benchIterator | ||
}; | ||
|
||
function main(conf) { | ||
var len = +conf.size; | ||
var clazz = conf.type === 'fast' ? Buffer : SlowBuffer; | ||
var buffer = new clazz(len); | ||
buffer.fill(0); | ||
|
||
methods[conf.method](buffer, conf.n); | ||
} | ||
|
||
|
||
function benchFor(buffer, n) { | ||
bench.start(); | ||
|
||
for (var k = 0; k < n; k++) | ||
for (var i = 0; i < buffer.length; i++) | ||
assert(buffer[i] === 0); | ||
|
||
bench.end(n); | ||
} | ||
|
||
function benchForOf(buffer, n) { | ||
bench.start(); | ||
|
||
for (var k = 0; k < n; k++) | ||
for (var b of buffer) | ||
assert(b === 0); | ||
|
||
bench.end(n); | ||
} | ||
|
||
function benchIterator(buffer, n) { | ||
bench.start(); | ||
|
||
for (var k = 0; k < n; k++) { | ||
var iter = buffer[Symbol.iterator](); | ||
var cur = iter.next(); | ||
|
||
while (!cur.done) { | ||
assert(cur.value === 0); | ||
cur = iter.next(); | ||
} | ||
|
||
} | ||
|
||
bench.end(n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
var common = require('../common'); | ||
var assert = require('assert'); | ||
|
||
var buffer = new Buffer([1, 2, 3, 4, 5]); | ||
var arr; | ||
var b; | ||
|
||
// buffers should be iterable | ||
|
||
arr = []; | ||
|
||
for (b of buffer) | ||
arr.push(b); | ||
|
||
assert.deepEqual(arr, [1, 2, 3, 4, 5]); | ||
|
||
|
||
// buffer iterators should be iterable | ||
|
||
arr = []; | ||
|
||
for (b of buffer[Symbol.iterator]()) | ||
arr.push(b); | ||
|
||
assert.deepEqual(arr, [1, 2, 3, 4, 5]); | ||
|
||
|
||
// buffer#values() should return iterator for values | ||
|
||
arr = []; | ||
|
||
for (b of buffer.values()) | ||
arr.push(b); | ||
|
||
assert.deepEqual(arr, [1, 2, 3, 4, 5]); | ||
|
||
|
||
// buffer#keys() should return iterator for keys | ||
|
||
arr = []; | ||
|
||
for (b of buffer.keys()) | ||
arr.push(b); | ||
|
||
assert.deepEqual(arr, [0, 1, 2, 3, 4]); | ||
|
||
|
||
// buffer#entries() should return iterator for entries | ||
|
||
arr = []; | ||
|
||
for (var b of buffer.entries()) | ||
arr.push(b); | ||
|
||
assert.deepEqual(arr, [ | ||
[0, 1], | ||
[1, 2], | ||
[2, 3], | ||
[3, 4], | ||
[4, 5] | ||
]); |