Skip to content

Instantly share code, notes, and snippets.

@mcollina
Created July 28, 2017 22:45
Show Gist options
  • Save mcollina/b903a1ef99f872ceb8e51dc491be34b8 to your computer and use it in GitHub Desktop.
Save mcollina/b903a1ef99f872ceb8e51dc491be34b8 to your computer and use it in GitHub Desktop.
bench-object-creation
'use strict'
var benchmark = require('benchmark')
var suite = new benchmark.Suite()
var runs = 0
class MyClass {
constructor (x) {
this.x = x
}
}
function MyCtor (x) {
this.x = x
}
var res = 0
suite.add('literal', function base () {
res = 0
var obj = { x: 1 }
res = obj.x
})
suite.add('class', function allNums () {
res = 0
var obj = new MyClass(1)
res = obj.x
})
suite.add('constructor', function allNums () {
res = 0
var obj = new MyCtor(1)
res = obj.x
})
var literal = { x: 1 }
suite.add('create', function allNums () {
res = 0
var obj = Object.create(literal)
res = obj.x
})
suite.on('cycle', () => runs = 0)
suite.on('complete', require('./print'))
suite.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment