Skip to content

Instantly share code, notes, and snippets.

@davedx
Created January 4, 2018 14:04
Show Gist options
  • Save davedx/9a4003e589e6e80a0947c6d4896b7c00 to your computer and use it in GitHub Desktop.
Save davedx/9a4003e589e6e80a0947c6d4896b7c00 to your computer and use it in GitHub Desktop.
getter_perf
var A = {
_propA: 'fish',
_propB: `cat${this._propA}`
}
var B = {
_propA: 'fish',
_propB: 'cat',
get propA() {
return this._propA
},
get propB() {
return this._propB + this._propA
}
}
var C = {
_propA: 'fish',
_propB: 'cat',
propA: function () {
return this._propA
},
propB: function () {
return this._propB + this._propA
}
}
var R, S;
var its = 10000000
T = Date.now()
for (var i=0; i<its; i++) {
R = A._propA;
S = A._propB;
}
console.log(`Elapsed: ${Date.now()-T}`)
T = Date.now()
for (var i=0; i<its; i++) {
R = B.propA;
S = B.propB;
}
console.log(`Elapsed: ${Date.now()-T}`)
T = Date.now()
for (var i=0; i<its; i++) {
R = C.propA();
S = C.propB();
}
console.log(`Elapsed: ${Date.now()-T}`)
@davedx
Copy link
Author

davedx commented Jan 4, 2018

Daves-MacBook-Pro:~ dave$ node -v
v6.9.2

Daves-MacBook-Pro:~ dave$ node getter_perf.js
Elapsed: 96
Elapsed: 2237
Elapsed: 186

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment