Skip to content

Instantly share code, notes, and snippets.

@reconbot
Created October 8, 2018 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reconbot/4d505d82e0122177a0054f5dcaf3100d to your computer and use it in GitHub Desktop.
Save reconbot/4d505d82e0122177a0054f5dcaf3100d to your computer and use it in GitHub Desktop.
const Benchmark = require('benchmark')
const Bl = require('./bl')
Bl.prototype.oldSlice = function slice (start, end) {
if (typeof start == 'number' && start < 0){
start += this.length
}
if (typeof end == 'number' && end < 0){
end += this.length
}
return this.copy(null, 0, start, end)
}
const makeArray = num => new Array(num).fill(1)
const smallBuffers = new Bl(makeArray(100).map(() => Buffer.alloc(8, 1)))
const smallBuffersLong = new Bl(makeArray(1000).map(() => Buffer.alloc(8, 1)))
const largeBuffers = new Bl(makeArray(100).map(() => Buffer.alloc(1024, 1)))
const mixedBuffers = new Bl(makeArray(100).map((val, i) => Buffer.alloc(i ** 2 % 1024, 1)))
const veryLargeBuffers = new Bl(makeArray(100).map(() => Buffer.alloc(1024 * 1024, 1)))
const notFindableSmall = Buffer.alloc(7, 0)
const notFindableMedium = Buffer.alloc(510, 0)
const notFindableLarge = Buffer.alloc(1025, 0)
const suite = new Benchmark.Suite()
// suite.add('get performance', () => {
// smallBuffers.get(2)
// })
suite.add('oldSlice', () => {
smallBuffers.oldSlice()
smallBuffers.oldSlice(0, 7)
veryLargeBuffers.oldSlice(0, 1024)
})
suite.add('slice', () => {
smallBuffers.slice()
smallBuffers.slice(0, 7)
veryLargeBuffers.slice(0, 1024)
})
// suite.add('smallBuffers small search', () => {
// smallBuffers.indexOf(notFindableSmall)
// })
// suite.add('smallBuffers medium search', () => {
// smallBuffers.indexOf(notFindableMedium)
// })
// suite.add('smallBuffers large search', () => {
// smallBuffers.indexOf(notFindableLarge)
// })
// suite.add('smallBuffersLong small search', () => {
// smallBuffersLong.indexOf(notFindableSmall)
// })
// suite.add('smallBuffersLong medium search', () => {
// smallBuffersLong.indexOf(notFindableMedium)
// })
// suite.add('smallBuffersLong large search', () => {
// smallBuffersLong.indexOf(notFindableLarge)
// })
// suite.add('largeBuffers small search', () => {
// largeBuffers.indexOf(notFindableSmall)
// })
// suite.add('largeBuffers medium search', () => {
// largeBuffers.indexOf(notFindableMedium)
// })
// suite.add('largeBuffers large search', () => {
// largeBuffers.indexOf(notFindableLarge)
// })
// suite.add('mixedBuffers small search', () => {
// mixedBuffers.indexOf(notFindableSmall)
// })
// suite.add('mixedBuffers medium search', () => {
// mixedBuffers.indexOf(notFindableMedium)
// })
// suite.add('mixedBuffers large search', () => {
// mixedBuffers.indexOf(notFindableLarge)
// })
// suite.add('veryLargeBuffers small search', () => {
// veryLargeBuffers.indexOf(notFindableSmall)
// })
// suite.add('veryLargeBuffers medium search', () => {
// veryLargeBuffers.indexOf(notFindableMedium)
// })
// suite.add('veryLargeBuffers large search', () => {
// veryLargeBuffers.indexOf(notFindableLarge)
// })
suite.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('done');
})
.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment