Skip to content

Commit

Permalink
benchmark: add Buffer.isBuffer
Browse files Browse the repository at this point in the history
TimothyGu committed Sep 21, 2017
1 parent cd1b55a commit 4fceb19
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions benchmark/buffers/buffer-isbuffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';
const common = require('../common');
const { Buffer } = require('buffer');

function FakeBuffer() {}
FakeBuffer.prototype = Object.create(Buffer.prototype);

// Refs: https://github.com/nodejs/node/issues/9531#issuecomment-265611061
class ExtensibleBuffer extends Buffer {
constructor() {
super(new ArrayBuffer(0), 0, 0);
Object.setPrototypeOf(this, new.target.prototype);
}
}

const inputs = {
primitive: false,
object: {},
fake: new FakeBuffer(),
subclassed: new ExtensibleBuffer(),
fastbuffer: Buffer.alloc(1),
slowbuffer: Buffer.allocUnsafeSlow(0),
uint8array: new Uint8Array(1)
};

const bench = common.createBenchmark(main, {
type: Object.keys(inputs),
n: [1e7]
});

function main(conf) {
const n = conf.n | 0;
const input = inputs[conf.type];

bench.start();
for (var i = 0; i < n; i++)
Buffer.isBuffer(input);
bench.end(n);
}

0 comments on commit 4fceb19

Please sign in to comment.