Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Is there an efficiency impact of using "arguments"

    I mean something like this:
    Code:
    function something()
    {
    	alert(arguments[0] + "," + arguments[1]);
    }
    Compared with doing this:
    Code:
    function something(arg1, arg2)
    {
    	alert(arg1 + "," + arg2);
    }
    Although slightly more physical code, I'd rather use the former, because it looks neater. But are there any runtime efficiency losses doing that?
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    I would suspect the latter of being faster by a very slight margin. However, why don't you benchmark it and see?
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    I would assume an array-lookup cost, which is minimal. The Javascript environment would create the arguments array whether or not you use it, so you incur the (lack of) performance cost of arrayName[0] versus variableName.

    But lioreans right, maybe the Javascript environment is whacky. Do benchmarks.

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Jason: arguments is a keyword. The engine might optimise those in a different way than identifiers. For example, there is no need for an identifier lookup on a keyword. A smart parser probably know that, so the question is whether the variable initialisation and identifier lookup is faster than doing just the array lookup and value confirmation. And I guess it depends a bit on the array implementation, too.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Test case:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    	<head>
    		<title>Testing</title>
    		<script language="javascript" type="text/javascript">
    			function test1()
    				{
    				return arguments[0] + arguments[1];
    				}
    			function test2(a,b)
    				{
    				return a + b;
    				}
    		</script>
    	</head>
    	<body>
    		<script language="javascript" type="text/javascript">
    			var StartTime1 = new Date();
    			for (var i = 1; i < 300000 ; i++)
    				{
    				test1(1,i)
    				}
    			var EndTime1 = new Date();
    			var StartTime2 = new Date();
    			for (var i = 1; i < 300000 ; i++)
    				{
    				test2(1,i)
    				}
    			var EndTime2 = new Date();
    			document.write("<hr><b>test1 code took " + ((EndTime1.getTime() - StartTime1.getTime()) / 1000) + " seconds<\/b><br>");
    			document.write("<hr><b>test2 code took " + ((EndTime2.getTime() - StartTime2.getTime()) / 1000) + " seconds<\/b><br>");
    		</script>
    	</body>
    </html>
    Firefox results: Both functions appear approximately equivalent, with multiple executions the times change slightly with one showing as quicker during one execution while the other shows as quicker during the next.

    IE Results: Test2 is significantly quicker (taking about 1/4 of the time of Test1) on a consistent basis.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks man - that saves me the time

    That's useful info then - leads me to conclude that for short scripts it doesn't matter, but for more complex scripts IE would be better off with the latter, if caring about IE is an issue at the time ...
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •