Hacker News new | past | comments | ask | show | jobs | submit login

Trying to understand the described matter I created some simple code to benchmark different cases.

Probably I got something wrong, but setting properties inside the constructor function (prop-benchmark-b.js) are almost 6 times slower that setting them outside of it (prop-benchmark-a.js)

https://gist.github.com/senotrusov/597514c773089b6989c6

What do you think, why I got such a different numbers?

Thank you!




There is a bug in your code:

    var o = new F(i, i, i, i, i, i, i, i, i, i, z)
    z = o
    // ... skipped 
    o.z = z
This makes o.z = o, so only the last object survives. When you use constructor you actually link all of them together - which means there is more garbage around - GCs are more expensive.

If you fix this bug, e.g. by doing

    var o = new F(i, i, i, i, i, i, i, i, i, i, z)
    o.z = z
    z = o
you actually OOM in prop-benchmark-a.js case because object there is less compact than one created by constructor. Just as the post predicts.


Thank you!

I was lucky to not hit OOM and I've got 5.648s for setting properties outside of the constructor and 3.680s for setting them inside of the constructor, so just as the post predicts.

At the same time, if I'm trying to simplify the test case and set only numeric values and not object ones, I've got nearly the same results: 3.953s and 3.922s for 10x more iterations (prop-benchmark-f.js and prop-benchmark-g.js).

(node 4.0.0 on Mac OS)


> At the same time, if I'm trying to simplify the test case and set only numeric values and not object ones, I've got nearly the same results: 3.953s and 3.922s for 10x more iterations (prop-benchmark-f.js and prop-benchmark-g.js).

Well, the secret here is that even if you have an empty constructor V8 still initially gives enough slack to you object for 10 in-object properties, and by coincidence you have precisely 10 properties.

Just add one more and observe the difference.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: