Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add string decoding benchmarks #352

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions perf/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var benchmark = require('benchmark')

exports.suite = function () {
exports.suite = function (isAsync) {
var suite = new benchmark.Suite()
process.nextTick(function () {
suite
Expand All @@ -13,7 +13,7 @@ exports.suite = function () {
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').map('name'))
})
.run({ async: true })
.run({ async: isAsync === undefined ? true : isAsync })
})
return suite
}
50 changes: 50 additions & 0 deletions perf/write-base64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict'

var BrowserBuffer = require('../').Buffer // (this module)
var util = require('./util')
var suite1 = util.suite(false)
var suite2 = util.suite(false)
var suite3 = util.suite(false)

var LENGTH = (8192 * 3) >>> 2
var browserSubject = BrowserBuffer.alloc(LENGTH)
var nodeSubject = Buffer.alloc(LENGTH)

var charset =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'

var str = ''

for (var i = 0; i < 8192; i++) {
str += charset[Math.random() * charset.length | 0]
}

suite1.add('BrowserBuffer.byteLength(' + str.length + ', "base64")', function () {
BrowserBuffer.byteLength(str, 'base64')
})

if (!process.browser) {
suite1.add('NodeBuffer.byteLength(' + str.length + ', "base64")', function () {
Buffer.byteLength(str, 'base64')
})
}

suite2.add('BrowserBuffer#write(' + str.length + ', "base64")', function () {
browserSubject.write(str, 'base64')
})

if (!process.browser) {
suite2.add('NodeBuffer#write(' + str.length + ', "base64")', function () {
nodeSubject.write(str, 'base64')
})
}

suite3.add('BrowserBuffer#toString(' + str.length + ', "base64")', function () {
browserSubject.toString('base64')
})

if (!process.browser) {
suite3.add('NodeBuffer#toString(' + str.length + ', "base64")', function () {
nodeSubject.toString('base64')
})
}
25 changes: 25 additions & 0 deletions perf/write-binary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict'

var BrowserBuffer = require('../').Buffer // (this module)
var util = require('./util')
var suite = util.suite()

var LENGTH = 8192
var browserSubject = BrowserBuffer.alloc(LENGTH)
var nodeSubject = Buffer.alloc(LENGTH)

var str = ''

for (var i = 0; i < LENGTH; i++) {
str += String.fromCharCode(Math.random() * 0x100 | 0)
}

suite.add('BrowserBuffer#write(' + str.length + ', "binary")', function () {
browserSubject.write(str, 'binary')
})

if (!process.browser) {
suite.add('NodeBuffer#write(' + str.length + ', "binary")', function () {
nodeSubject.write(str, 'binary')
})
}
33 changes: 18 additions & 15 deletions perf/write-hex.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
const BrowserBuffer = require('../').Buffer // (this module)
const util = require('./util')
const suite = util.suite()
'use strict'

const LENGTH = 4096
const browserSubject = BrowserBuffer.alloc(LENGTH)
const nodeSubject = Buffer.alloc(LENGTH)
var BrowserBuffer = require('../').Buffer // (this module)
var util = require('./util')
var suite = util.suite()

const charset = '0123456789abcdef'
var LENGTH = 4096
var browserSubject = BrowserBuffer.alloc(LENGTH)
var nodeSubject = Buffer.alloc(LENGTH)

let str = ''
var charset = '0123456789abcdef'

for (let i = 0; i < LENGTH * 2; i++)
var str = ''

for (var i = 0; i < LENGTH * 2; i++) {
str += charset[Math.random() * charset.length | 0]
}

suite
.add('BrowserBuffer#write(' + LENGTH + ', "hex")', function () {
browserSubject.write(str, 'hex')
})
suite.add('BrowserBuffer#write(' + str.length + ', "hex")', function () {
browserSubject.write(str, 'hex')
})

if (!process.browser) suite
.add('NodeBuffer#write(' + LENGTH + ', "hex")', function () {
if (!process.browser) {
suite.add('NodeBuffer#write(' + str.length + ', "hex")', function () {
nodeSubject.write(str, 'hex')
})
}
25 changes: 25 additions & 0 deletions perf/write-ucs2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict'

var BrowserBuffer = require('../').Buffer // (this module)
var util = require('./util')
var suite = util.suite()

var LENGTH = 16384
var browserSubject = BrowserBuffer.alloc(LENGTH)
var nodeSubject = Buffer.alloc(LENGTH)

var str = ''

for (var i = 0; i < LENGTH / 2; i++) {
str += String.fromCharCode(Math.random() * 0x10000 | 0)
}

suite.add('BrowserBuffer#write(' + str.length + ', "ucs2")', function () {
browserSubject.write(str, 'ucs2')
})

if (!process.browser) {
suite.add('NodeBuffer#write(' + str.length + ', "ucs2")', function () {
nodeSubject.write(str, 'ucs2')
})
}
Loading