Skip to content

Instantly share code, notes, and snippets.

@slugbyte
Created December 17, 2018 17:10
Show Gist options
  • Select an option

  • Save slugbyte/abb3b64c7bab386d637cb959b76d56a6 to your computer and use it in GitHub Desktop.

Select an option

Save slugbyte/abb3b64c7bab386d637cb959b76d56a6 to your computer and use it in GitHub Desktop.
// worst names ever but it shows that instantionation with the new keyword is many orders of magnitude faster than
// using object.create or object.setPrototypeOf or Object.defineProperties :(
// WHYYYYYY
class cls {
constructor(){
this.x = 'x'
this.y = 'y'
this.z = 'z'
}
}
cls.prototype.a = 'a'
makeClassObject = () => {
return new cls()
}
function obj(){
this.x = 'x'
this.y = 'x'
this.z = 'x'
}
obj.prototype = { a: 'a' }
makeDumbObject = () => {
return new obj()
}
makeObjectsMutate = () => {
let result = Object.create({a: 'a'})
result.x = 'x'
result.y = 'y'
result.z = 'z'
return result
}
makeObjectsSetProto = () => {
result = {
x: 'x',
y: 'y',
x: 'x',
}
Object.setPrototypeOf(result, {a: 'a'})
return result
}
makeObjectsDefine = () => {
return Object.create({a: 'a'}, {
x: {
value: 'x',
writable: true,
},
y: {
value: 'x',
writable: true,
},
z: {
value: 'x',
writable: true,
},
})
}
big = Math.pow(2, 20)
for(x=0; x< 5; x++){
console.time('cool')
for(i=0; i<big; i++){
makeObjectsSetProto()
}
console.timeEnd('cool')
}
for(x=0; x< 5; x++){
console.time('cool')
for(i=0; i<big; i++){
makeObjectsMutate()
}
console.timeEnd('cool')
}
for(x=0; x< 5; x++){
console.time('cool')
for(i=0; i<big; i++){
makeObjectsDefine()
}
console.timeEnd('cool')
}
for(x=0; x< 5; x++){
console.time('cool')
for(i=0; i<big; i++){
makeDumbObject()
}
console.timeEnd('cool')
}
for(x=0; x< 5; x++){
console.time('cool')
for(i=0; i<big; i++){
makeClassObject()
}
console.timeEnd('cool')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment