Implicit Vs. Explicit Conversion in Javascript
January 14th, 2008A question emerged out of my boredom on my flight yesterday. Which is faster, implicit vs explicit conversion. Most javascript developers use implicit conversion out of habit. For example:
!!x; instead of Boolean( x ); and x + “”; instead of String( x);.
I decided to try an experiment for myself and record the performance of casting a number to a Boolean or String on my Windows box in Safari 3, Firefox 2, Opera 9 and IE 7.
The Verdict:
Implicit conversion wins handily, demonstrating over a 7 fold performance increase in one test. Overall, the performance gain for using implicit conversion averaged out to 53% across browsers after 10 tests.
The Numbers:
| Implicit Boolean | Explicit Boolean | Implicit String | Explicit String | |
| Firefox 2 | 0.162 | 0.312 | 0.248 | 0.358 |
| IE 7 | 0.042 | 0.100 | 0.074 | 0.152 |
| Opera 9 | 0.030 | 0.088 | 0.020 | 0.142 |
| Safari 3 | 0.028 | 0.036 | 0.074 | 0.100 |
| Cross Browser Average | 0.066 | 0.134 | 0.104 | 0.188 |
You can find the code I used after the jump.
The Code:
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=windows-1250″>
<meta name=”generator” content=”PSPad editor, www.pspad.com”>
<title></title>
<script type=”text/javascript”>
// JavaScript Document
var implicitBoolean, explicitBoolean, implicitString, explicitString, startDate, endDate;
implicitBoolean = explicitBoolean = implicitString = explicitString = 0;
for( i = 0; i < 500; i++ ){
startDate= new Date();
for( i=0; i < 32000; i++){ Boolean( i );}
endDate = new Date();
explicitBoolean += ( endDate – startDate );
startDate= new Date();
for( i=0; i < 32000; i++){ !!i;}
endDate = new Date();
implicitBoolean += ( endDate – startDate );
startDate= new Date();
for( i=0; i < 32000; i++){ String( i );}
endDate = new Date();
explicitString += ( endDate – startDate );
startDate= new Date();
for( i=0; i < 32000; i++){ i + “”;}
endDate = new Date();
implicitString += ( endDate – startDate );
}
document.writeln( “Implicit Boolean: ” + (implicitBoolean / 500 ) + “<br />” );
document.writeln( “Explicit Boolean: ” + (explicitBoolean / 500 ) + “<br />” );
document.writeln( “Implicit String: ” + (implicitString / 500 ) + “<br />” );
document.writeln( “Explicit String: ” + (explicitString / 500 ) + “<br />” );
</script>
</head>
<body>
</body>
</html>


Previous Post
Next Post

2 Comments
January 19th, 2008 at 12:36 am
Curious, it’s kind of expected as ‘Boolean(x)’ has to look up the Boolean constructor dynamically, but then !!x requires two JS operations which obviously aren’t as cheap as they are natively
June 2nd, 2008 at 1:40 pm
[...] of Boolean x and x + ?????? instead of String x … Explicit Boolean: implicit String: Explicit …http://www.ajaxbestiary.com/2008/01/14/implicit-vs-explicit-conversion-in-javascript/BOO-80 implicit conversion from single char string literal to …… implicit conversion from single [...]