views:

132

answers:

6

Of these similar function definitions, what is optimal way to access arguments and why?

function DoStuff()
{
    return arguments[0] + arguments[1] + arguments[2];
}

function DoStuff(first, second, third)
{
    return first + second + third;
}

Does one allocate less memory than the other? Is one faster to access the parameter values than the other?

+1  A: 

The second.

Using the second, you know based on the signature exactly what is expected when you call the method. It's also far easier for maintainability in the future.

Justin Niessner
A: 

The performance difference should be fairly negligible between the two, however the readability of named parameters is a better practice. There are times when you cannot use named parameters and in those cases I typically use a big block of script to unpack arguments into named locals.

Bryan Kyle
+7  A: 

Forget about performance in this case and go for readability. From that perspective, option (2) is much to be preferred -- though, I'd go for more descriptive names. ;-)

tvanfosson
This isn't an answer to the question. Does one allocate less memory than the other? Is one faster to access the parameter values than the other?
Crescent Fresh
Yes. It does answer **the** question, though not all of the questions asked in the post. The optimal way to reference parameters is the most readable way.
tvanfosson
Did you get the *point* of the question though? It's an interesting question, if only from an "academic exercise" POV.
Crescent Fresh
@Crescent Fresh -- I don't want to get in a big fight about this, but did you get the point of my answer. The memory foot print and speed considerations for this are so far below the importance of good naming and readability that it wouldn't matter if the less readable version was faster.
tvanfosson
@tvanfosson: absolutely I get the point of your answer. In fact I agree with you wholeheartedly. It just missed the point of the question IMO. Again, it would still be interesting to see an analysis of this somewhere as a way to better understand JavaScript engines (particularly non-opensourced ones, i.e. JScript) and to make SO the de-facto knowledge base on this kind of thing. It has no real-world implications as you point out.
Crescent Fresh
@Crescent Fresh -- and other downvoters -- notice that the question is tagged "best practices". I have a hard time believing that people really feel that focusing on this micro-optimization is really more of a best practice than good naming and readability.
tvanfosson
@tvanfosson: fair enough. Can you edit so I can undownvote?
Crescent Fresh
@Crescent Fresh -- done, and thank you.
tvanfosson
He didn't ask what was most readable.
Nosredna
As I said, though, he did tag it best practices. In this case, the best practice is not caring what the performance difference is (though, it turns out that explicit parameters are faster -- see Shog9's comment on the question).
tvanfosson
A: 

Your junior colleague in the adjacent cube will thank you when you decide to use the latter.

Upper Stage
Your senior colleague will, too.
Chris Farmer
+7  A: 

Here is my test:

    function Test1()
    {
        var start = new Date();
        for (var i = 0; i < 1000000; i++)
        {
            DoStuff1(i, i + 1, i + 2);
        }
        var done = new Date();

        alert(done - start);
    }

    function Test2()
    {
        var start = new Date();
        for (var i = 0; i < 1000000; i++)
        {
            DoStuff2(i, i + 1, i + 2);
        }
        var done = new Date();

        alert(done - start);
    }

    function DoStuff1()
    {
        var result = arguments[0] + arguments[1] + arguments[2];
    }

    function DoStuff2(first, second, third)
    {
        var result = first + second + third;
    }

Here are the results:

IE   FF

Test1()
2355    402
2381    395
2368    392

Test2()
504  6
507  7
505  7

I figured that test 2 would be faster but it's drastically faster. So, not only is it more readable but it's more efficient.

Matthew
I wondered if anyone was going to answer the question asked.
Nosredna
Is it IE8? It illustrates the point I wanted to make - javascript interpreters have radically different performance. And new engines come out all the time.
Alexander Abramov
To put it into perspective, would you measure the time it takes to $(this) with jQuery for example? 2ms per invocation (IE case) could actually do a difference for library writers.
Alexander Abramov
+2  A: 

Referencing arguments[] anywhere in a function will significantly decrease performance on many browsers.

Andy
Heh, relative to what?
Crescent Fresh
Heh, yeah, it'll *decrease* performance; whether the decrease is *significant* or not depends on your program. I would love to see a non-contrived example where it actually *matters*...
Shog9