29

I want to understand why is the difference in performance when both does same thing?

enter image description here

Benchmark

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
Deepak Ingole
  • 14,912
  • 10
  • 47
  • 79

1 Answers1

35

Performance of {} can be explained as :

  1. {} is the literal for object in Javascript, and literals are evaluated faster.
  2. As an added bonus, literals take up less space in your code, so the overall file size is
    smaller.
  3. The end result of literal code is the same as the new Object() code, but it is executed faster in almost all browsers (Firefox 3.5 shows almost no difference).
  4. As the number of object properties and array items increases, so too does the benefit of using literals.

Object Literals {} are executed faster because of scope managing mechanism in Javascript

When JavaScript code is being executed, an execution context is created. The execution context (also sometimes called the scope) defines the environment in which code is to be executed.

A global execution context is created upon page load, and additional execution contexts are created as functions are executed, ultimately creating an execution context stack where the topmost context is the active one.

Each execution context has a scope chain associated with it, which is used for identifier resolution. The scope chain contains one or more variable objects that define in-scope identifiers for the execution context.

The global execution context has only one variable object in its scope chain, and this object defines all of the global variables and functions available in JavaScript.

When a function is created (but not executed), its internal [[Scope]] property is assigned to contain the scope chain of the execution context in which it was created (internal properties cannot be accessed through JavaScript, so you cannot access this property directly).

Later, when execution flows into a function, an activation object is created and initialized with values for this, arguments, named arguments, and any variables local to the function. The activation object appears first in the execution context’s scope chain and is followed by the objects contained in the function’s [[Scope]] property.

During code execution, identifiers such as variable and function names are resolved by searching the scope chain of the execution context.

Identifier resolution begins at the front of the scope chain and proceeds toward the back. Consider the following code:

function Add(n1, n2) {
  this.n1 = n1;
  this.n2 = n2;
  this.val = this.n1 + this.n2;
}

var result = new Add(5, 10);

When this code is executed, the add function has a [[Scope]] property that contains only the global variable object.

As execution flows into the add function, a new execution context is created, and an activation object containing this, arguments, n1, and n2 is placed into the scope chain.

Below Figure , “Relationship of execution context and scope chain” illustrates the behind-the-scenes object relationships that occur while the add function is being executed.

enter image description here

Inside the add function, the identifiers num1 and num2 need to be resolved when the function is executing.

This resolution is performed by inspecting each object in the scope chain until the specific identifier is found.

The search begins at the first object in the scope chain, which is the activation object containing the local variables for the function.

If the identifier isn’t found there, the next object in the scope chain is inspected for the identifier. When the identifier is found, the search stops.

In the case of this example, the identifiers num1 and num2 exist in the local activation object and so the search never goes on to the global object.

Understanding scopes and scope chain management in JavaScript is important because identifier resolution performance is directly related to the number of objects to search in the scope chain.

The farther up the scope chain an identifier exists, the longer the search goes on and the longer it takes to access that variable; if scopes aren’t managed properly, they can negatively affect the execution time of your script.

Mazzu
  • 2,799
  • 20
  • 30
  • 3
    Pilot, Please find my updates in above answer under Performance heading. – Mazzu Feb 04 '14 at 09:09
  • {} is literal and literals are evaluated faster in javascript, but new Object() is a constructor call to function Object() which is having its own definition that takes comparatively more time to evaluate. – Mazzu Feb 04 '14 at 11:07
  • While creating object through {} we are actually declaring it and In javascript, declarations are processed first apart from functions. – Mazzu Feb 04 '14 at 12:06
  • Hey Pilot, are you fine with the explanation? – Mazzu Feb 06 '14 at 15:10
  • YOu explained all well but cause is missing my friend..Anyways I already upvoted u...You quote `{} is the literal for object in Javascript, and literals are evaluated faster.` but forgot to put the reason..you quote `The end result of literal code is the same as the new Object() code, but it is executed faster in almost all browsers ` and dont explain why? .. Not acceptable ans so not marking as correct..Please try and understand question..I want **cause **.So ans should be **Performance diff is because...** – Deepak Ingole Feb 06 '14 at 16:32
  • Pilot, please find the explanation in above answer under "Further more explanation :" section. Hope this can be helpful for you :) – Mazzu Feb 07 '14 at 15:11
  • Pilot, you can go through the updates in above answer – Mazzu Feb 11 '14 at 14:38
  • Pilot, fine with the explanation? – Mazzu Feb 12 '14 at 12:06
  • Please scrub your ans please...I request..remove irrelevant part..Keep your ans within scope of question..and question is about performance diff and not about `new Object()` and `{}` works – Deepak Ingole Feb 13 '14 at 16:05
  • Pilot answer is edited. Are you fine with explanation? – Mazzu Feb 14 '14 at 13:16
  • Would you please source when you copy someone verbatim.... – megawac Feb 14 '14 at 13:26
  • 2
    There is a really good point made in this answer: http://stackoverflow.com/questions/4597926/creating-objects-new-object-or-object-literal-notation -- In summary, you can set up a function to create instances form literal notation or constructors. Creating instances with literal notation, each instance carries all the methods, whereas with a constructor all instances refer to the prototype methods. Ie constructor has better memory performance when creating many instances. – Federico Dec 17 '14 at 22:44
  • Much of this answer is taken directly from _[Even Faster Websites](http://archive.oreilly.com/pub/a/server-administration/excerpts/even-faster-websites/writing-efficient-javascript.html)_, a book written by Steve Souders and others, so you can see that for more information. (I'm not sure whether it addresses the OP's question about {} vs new Object() directly, though.) – insectean Jul 19 '16 at 17:36
  • @Brian, thanks for your comment. Happy to see that it help you out :) – Mazzu Dec 05 '16 at 05:38
  • So the literal way is faster because there is no need for constructor function retrieving underhood. – ladjzero Aug 08 '18 at 02:51