You should

1. Use switch statement and literal character codes
  or 
2. Cache the reference character codes in closured variables
  or
3. Obviously: Use a regular expression!

Good catch on the hash-miss thing - stored another 
good-to-remeber-in-algorithm-choice snippet in my head.

On Wednesday, July 7, 2010 2:54:10 PM UTC+2, Chad wrote:
>
> I was surprised to find that a function call returning a boolean from 
> a simple test was much (3x!) faster than an associative array 
> lookup... I was wondering if someone could explain to me how that 
> could be the case. Is v8 inlining the function somehow, or is the 
> overhead of a function call extremely low? 
>
> Note 1: I'm not complaining, I'm just curious as to how things were 
> working under the covers. 
> Note 2: The opposite is true for Firefox, which is 2x slower using the 
> function call than the associative array... so an optimization for one 
> would result in a performance regression for another... :( 
>
> Example: 
>
> ------ 
> isWhitespace:function(charCode) { 
>   return charCode == this.spaceCharCode || charCode == 
> this.tabCharCode || ... etc ...; 
> }, 
>
> for (var i = 0; i < aString.length; i++) { 
>   var isWs = this.isWhitespace(aString.charCodeAt(i)); 
> } 
> ------ 
>
> Is 3x faster than: 
>
> ------ 
> whitespace: Associative array mapping whitespace char codes to true, 
>
> for (var i = 0; i < aString.length; i++) { 
>   var isWs = this.whitespace[aString.charCodeAt(i)]; 
> } 
> ------ 
>
> Thanks in advance, Chad 
>

-- 
-- 
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
--- 
You received this message because you are subscribed to the Google Groups 
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to