Local variable reference to object property overhead in JavaScript

luleksde

Is it a bad practice to use a local variable as reference to an object property instead of the property itself?

function Apple(){
  this.type = 'green appale';
}

Apple.prototype.slice = function(){
  //Does this create a noticeable overhead?
  var type = this.type;

  //slicing action

}
Sukima

The answer is both yes and no. From a performance stand point don't worry about it. This is a cake walk compared to other performance issues. Besides the rule of thumb is to write code for readability and only optimize when you need to through profiling after the fact. Don't pre optimize.

As for the caveats… If the value you are saving as an immutable value (string, int, boolean) then your are 100% safe do do this. If it is an object or array then your need to know that it is a reference and changes will affect the original variable as well. And finally if it is a function then you have to be aware that you will loose it's context in the code example above.

function foo() {}

var bar = foo; // Context is irrelevant.

function foo() {
  this.baz = 'foobar';
}

var bar = foo; // You must provide a context or `this` becomes `window`.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is there a way to set a variable in JavaScript equal to the property of object as a reference?

From Dev

Store a reference to a property of an object in a variable

From Dev

Store a reference to a property of an object in a variable

From Dev

Reference object property through variable

From Dev

Javascript create reference to an object property?

From Dev

Any way to assign a variable (not an object property) another variable's value by reference in JavaScript?

From Dev

Javascript object property with variable name

From Dev

Javascript object property with variable name

From Java

javascript-local variable reference before assign

From Dev

Javascript using local variable for function property string

From Java

How to copy JavaScript object to new variable NOT by reference?

From Dev

how to reference a variable from the parent object in javascript

From Dev

How to reference an object property when defining an object literal in Javascript?

From Dev

How do I change an object property via a reference variable?

From Dev

variable object property name as parameter in javascript?

From Dev

Javascript object property not updating on variable set

From Dev

Bind scope variable with a Javascript object property

From Dev

Escaping reference to local variable

From Dev

Reference behavior for property of an object

From Dev

JavaScript: Convert string and concatenate it to dynamically create an object property reference

From Dev

Reference variable parameter vs local reference variable

From Dev

Javascript bad practice to save reference of object or function as a variable for anonymous function

From Dev

Why does a Javascript variable not have an object pass by reference?

From Dev

Assigning reference argument to local variable

From Dev

Find reference of local variable in vim

From Dev

JavaScript: Event Handlers: Where to declare variables - local or closure (vs overhead)?

From Dev

Returning reference to local temporary object

From Dev

JavaScript: Access object multi-level property using variable

From Dev

Javascript creating a normal variable fallback if object property does not exist

Related Related

  1. 1

    Is there a way to set a variable in JavaScript equal to the property of object as a reference?

  2. 2

    Store a reference to a property of an object in a variable

  3. 3

    Store a reference to a property of an object in a variable

  4. 4

    Reference object property through variable

  5. 5

    Javascript create reference to an object property?

  6. 6

    Any way to assign a variable (not an object property) another variable's value by reference in JavaScript?

  7. 7

    Javascript object property with variable name

  8. 8

    Javascript object property with variable name

  9. 9

    javascript-local variable reference before assign

  10. 10

    Javascript using local variable for function property string

  11. 11

    How to copy JavaScript object to new variable NOT by reference?

  12. 12

    how to reference a variable from the parent object in javascript

  13. 13

    How to reference an object property when defining an object literal in Javascript?

  14. 14

    How do I change an object property via a reference variable?

  15. 15

    variable object property name as parameter in javascript?

  16. 16

    Javascript object property not updating on variable set

  17. 17

    Bind scope variable with a Javascript object property

  18. 18

    Escaping reference to local variable

  19. 19

    Reference behavior for property of an object

  20. 20

    JavaScript: Convert string and concatenate it to dynamically create an object property reference

  21. 21

    Reference variable parameter vs local reference variable

  22. 22

    Javascript bad practice to save reference of object or function as a variable for anonymous function

  23. 23

    Why does a Javascript variable not have an object pass by reference?

  24. 24

    Assigning reference argument to local variable

  25. 25

    Find reference of local variable in vim

  26. 26

    JavaScript: Event Handlers: Where to declare variables - local or closure (vs overhead)?

  27. 27

    Returning reference to local temporary object

  28. 28

    JavaScript: Access object multi-level property using variable

  29. 29

    Javascript creating a normal variable fallback if object property does not exist

HotTag

Archive