[Javascript] Single vs. Double quotes in Javascript

Peter Brunone peter at brunone.com
Thu Nov 13 10:27:59 CST 2003


>It proves that string operations with single quoted strings are faster
>than with double quoted strings, exactly what I initially claimed.

    Actually, you originally claimed that single quotes were many orders of magnitude faster.  Upon testing your code however, I found the following results:

1)  The second test, single quotes, came in about about 9.5% faster than the first test, double quotes.

2)  When I reversed the order of the tests, double quotes were actually 1%-3% faster than single quotes!

   Basically, this goes to show that looping 100000x (which doesn't really duplicate any real-world situation) isn't going to give you sufficient reason to code one way or another, unless the resulting spread is more than a whole-number multiple of one of the values.  Either way, it's not the best test method, since there are many other outside factors and it's extremely unlikely that I'm ever going to have a comparable process to the one outlined in this exercise.

Cheers,

Peter Brunone

P.S.  The actual code I used can be found at http://aspdll.com/quotetest.html if anyone is interested; you'll probably get more fun and different results each time :)

Original Message:
>From: Håkan Magnusson <hakan at backbase.com>

>DHTML attributes?
>It proves that string operations with single quoted strings are faster
>than with double quoted strings, exactly what I initially claimed.
>
>
>Mike Dougherty wrote:
>
>> I did not test this code to confirm or deny the 15% faster claim
>> 
>> How does this test prove performance on access to single- vs.
>> double-quoted dhtml attributes?
>> 
>> (unless I missed the original point)
>> 
>> -----Original Message-----
>> From: javascript-bounces at LaTech.edu
>> 
>> Well, it's not a *billion* times faster, but this little simple test 
>> case proves it to be at least 15% faster.
>> 
>> -------
>> var s='';
>> var iTime1 = new Date().valueOf();
>> for(var i=0;i<100000;i++){
>> 	s+="a";
>> }
>> var iTime2 = new Date().valueOf();
>> alert('With double quote : ' + (iTime2 - iTime1));
>> 
>> 
>> s='';
>> iTime1 = new Date().valueOf();
>> for(var i=0;i<100000;i++){
>> 	s+='a';
>> }
>> iTime2 = new Date().valueOf();
>> alert('With single quote : ' + (iTime2 - iTime1));
>> -------
>> 
>> So no, I wasn't talking about Perl, I was talking about JavaScript, 
>> where there IS a difference.
>> 
>> 
>> Tim Makins wrote:
>> 
>>>Perhaps the person is getting mixed up with Perl, where there IS a
>>>difference :
>>>http://perl.about.com/library/weekly/aa052900a.htm
>>>
>>>Tim in Ireland.
>>>
>>>----- Original Message -----
>>>From: "Peter Brunone" <peter at brunone.com>
>>>
>>>>*I* still want to know why someone claimed that single quotes
>>>>parse a million times faster than double quotes.
>>>>
>>>>-Peter





More information about the Javascript mailing list