sookie
sookie

Reputation: 2517

JavaScript Performance: access time of properties - 'this' vs 'global object' vs 'variable'

var globalObject = { x: 0 }; // some global object with 'x' property

function Object()
{
    this.x = 0;
}

Object.prototype.TestPerformance = function()
{
    var xVar = 0;
}

From the function TestPerformance, what is the order of access time for these properties/variables?

this.x vs globalObject.x vs xVar

Upvotes: 2

Views: 151

Answers (1)

Ben Aston
Ben Aston

Reputation: 55729

Will be implementation dependent and is unknowable without looking at the engine source.

But best approximation:

this.x 

O(1) time/complexity

globalObject.x

O(1) time/complexity

Assuming LexicalEnvironments are searched at compile time.

var xVar

O(1) time/complexity

Upvotes: 2

Related Questions