ephemeron
ephemeron

Reputation: 396

Any performance difference between data from function arguments vs assigned variable?

This might sound like a noob question but here goes; Basically, I'm passing a large amount of data from one object to another. Below is a simplified example.

// Example 1
function Person(hugeData) {
    this.info = function() {
        console.log(hugeData);
    }
}
Homer = new Person(hugeData);
Homer.info();

Compared with

// Example 2
function Person() {
    var hugeData;

    this.set = function(data) {
        hugeData = data;
    }

    this.info = function() {
        console.log(hugeData);
    }
}
Homer = new Person();
Homer.set(hugeData);
Homer.info();

Is there much of a difference performance-wise between the two code snippets? Please focus on the context of the example rather than the code itself (setting object variable vs passing by arguments).

While the example above is for Javascript, I would also like to know if the same principle applies for other programming languages like PHP.

Thanks.

Upvotes: 2

Views: 374

Answers (4)

raina77ow
raina77ow

Reputation: 106385

I assume the major point of your question is whether this line...

hugeData = data;

... in your code may affect the performance.

And the answer is no, it's not (at least not so it can affect the application's performance).

If hugeData refers to an object (and remember arrays in JS are essentially objects), it actually stores only a reference to this object. And the reference is what will be copied, without any duplication of the object's contents.

If hugeData refers to a string, it can be a bit more complicated... but as far as I know, most modern browsers (check MDN, as example) now implement 'copy-on-writing' technique. In other words, the string won't be duplicated here as well.

Upvotes: 2

jAndy
jAndy

Reputation: 235972

No, not at all.

Without going into much detail now, both, formal paramters and local variables are stored within the such called Activation Object (in ES3) or the Lexical Environment Record (ES5) under the hood.

So access times should be identical by spec.


If you want to know the details, checkout:

http://dmitrysoshnikov.com/ecmascript/javascript-the-core/

and

http://dmitrysoshnikov.com/ecmascript/es5-chapter-3-2-lexical-environments-ecmascript-implementation/


Testcase: http://jsperf.com/formal-parameters-vs-local-variables-access-time

Upvotes: 4

Philipp
Philipp

Reputation: 69663

It's hard to generalize in this case because every implementation of Javascript is different.

I could imagine that when you pass a big hunk of data with the creation of an object, you are saving a reallocation of memory, which would happen when you create the object with few data first and then add a lot of data to it later. But most JS implementation will store the big data chunk in a reference anyway, so it is unlikely that it really matters.

Upvotes: 0

karthick
karthick

Reputation: 12176

Passing by variable is the best thing. Because it's obvious your huge data is not a primitive so you are going to only refer that in the functions context. Look at jAndy's answer.

Upvotes: 0

Related Questions