In JavaScript, which method of creating an object with properties is most efficient?

前端 未结 2 755
梦如初夏
梦如初夏 2020-12-21 03:13

I\'m a little new to JavaScript. I\'ve used it here and there on the client-side for a while, but now I am adventuring into server-side Javascript. This question is about Ja

2条回答
  •  独厮守ぢ
    2020-12-21 03:25

    Heed the advice of others about premature optimization - meaning don't focus on these sorts of implementation details which will likely vary greatly between different JavaScript interpreter implementations.

    However, based on this JSPerf benchmark and a sample size of one (on Chrome/Mac OS X), the object literal form (o = {foo:'bar'}) is much faster than setting properties after construction (o={}; o.foo='bar').

提交回复
热议问题