If you are still having problems getting both to be of similar speed, why not try using a switch statement?
On Jul 8, 7:38 am, Chad <[email protected]> wrote: > I hadn't considered the ramifications of a "miss" on a sparse array, > and since (in my test case) only 18% of the string being traversed was > white space, then there were a lot of "misses". > > Thanks for the great response - it helped a lot. > > Chad > > On Jul 8, 1:57 am, "Lasse R.H. Nielsen" <[email protected]> wrote: > > > > > I am guessing[1] it's just a matter of being very fast at calling functions > > vs. needing to do extra work for missing elements in the array. > > > I'm assuming your array is sparse (only have elements at the positions that > > are actually whitespace). > > That means that for all non-whitespace char-codes, you first fail to find > > the property in the whitespace array, and then you traverse the prototype > > chain and fail to find it in Array.prototype and Object.prototype as well. > > It's also likely that the sparse array is implemented using a hash-map, > > meaning that it has to do even more computation before it can fail to find > > the value. > > > The function call, on the other hand, always call the same function, so the > > Inline Cache will speed up the actual call a lot. > > > There is a prevalent superstition that function calls have to be slow. > > That's just not true. Compared to many other things a JavaScript engine has > > to do, function calls can be pretty quick. > > > /Lasse > > [1] from the code shown, it's impossible to be certain what bottlenecks are > > most significant.On Wed, Jul 7, 2010 at 15:01, Chad <[email protected]> > > wrote: > > > Sorry - should have specified the version that yielded these numbers > > > is: Chrome (Mac) v 5.0.375.99. > > > > On Jul 7, 7:54 am, Chad <[email protected]> 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 > > > > -- > > > Lasse R.H. Nielsen / <http://groups.google.com/group/v8-users> > > > [email protected] > > > 'Faith without judgement merely degrades the spirit divine' -- v8-users mailing list [email protected] http://groups.google.com/group/v8-users
