A source
define two objects with both getter and setter , with the same code
both tests with benchmark.js in node v7.3.0
const builtInObject1 = (function (object) { let lastA = 1; return Object.defineProperties(object, { a:{ get(){ return lastA }, set(newValue){ lastA = newValue; } } }) })({}); const builtInObject2 = (function (object) { let lastA = 1; return Object.defineProperties(object, { a:{ get(){ return lastA }, set(newValue){ lastA = newValue; } } }) })({});
~ add es2015 's getter/setter case
const builtInObject3 = (function () { let lastA = 1; return { get a(){ return lastA }, set a(value){ lastA = value; } } })();
~
const builtInObject4 = (function (object) { let last = 1; return Object.defineProperties(object, { b:{ get(){ return last }, set(newValue){ last = newValue; } } }) })({}); const builtInObject5 = (function (object) { let last = 1; return Object.defineProperties(object, { c:{ get(){ return last }, set(newValue){ last = newValue; } } }) })({});
~
(new Benchmark.Suite("object-assign-properties")) .add("#built-in object1.a getter and setter", function () { builtInObject1.a = builtInObject1.a + 1; }) .add("#built-in object2.a getter and setter", function () { builtInObject2.a = builtInObject2.a + 1; }) .add("#built-in object3.a es6 getter and setter", function () { builtInObject3.a = builtInObject3.a + 1; }) .add("#built-in object4.b getter and setter", function () { builtInObject4.b = builtInObject4.b + 1; }) .add("#built-in object5.c getter and setter", function () { builtInObject5.c = builtInObject5.c + 1; }) .on('cycle', function(event) { console.log(String(event.target)); }) .on('complete', function() { console.log('Fastest is ' + this.filter('fastest').map('name')); }) .run({ 'async': false });
result
the difference between ops/sec results of these tests makes me confused
Why???
what makes these differences?
accroding to bluebird | Optimization-killers , object3 won't be optimized, but why does object2 get so slow?
link???
- Jsperf tests, it seems both tests run with optimized code in my browser (osx Chrome Canary 57, CPU: i7), just got
3,800,000 opt / sec
related links