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.
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