JavaScript Performance: Multiple variables or one object?

后端 未结 4 679
没有蜡笔的小新
没有蜡笔的小新 2020-12-09 02:47

this is just a simple performance question, helping me understand the javascript engine. for this I\'m was wondering, what is faster: declaring multiple variables for certai

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 02:58

    You are definitely micro-optimizing. I wouldn't worry about it until there is a demonstrable performance bottleneck, and you have narrowed the issue to using multiple vars vs a object with properties.

    Logically thinking about it using the object approach requires three variable creations, one for the object, and one for each property on the object, vs 2 for just declaring variables. So having the object will have a higher memory approach. However, it is probably more efficient to pass an object to a method, than n > 1 variables to a method, since you only need to copy 1 value (javascript is pass by value). This also has implications for keeping track of the lexical scoping of the objects; i.e. passing less things to methods will use less memory.

    however, i doubt the performance differences will even be quantifiable by any profiler.

提交回复
热议问题