Execution efficiency of forEach function in google V8 engine

161 views
Skip to first unread message

刘玉龙

unread,
May 23, 2013, 4:37:10 AM5/23/13
to v8-u...@googlegroups.com
I found an interesting thing in V8 engine when I was testing underscore.js library.

For arrays, the 
for (var i = 0; i < len; i++)
implementation can ran 24700 ops(operations per second), while the native function 
Array.prototype.forEach
 only got 14200 ops.

Then I tried IE10, in which the `nativeForEach` can reach a number of 43500.

I checked the source code of V8 engine, but could not find out why this native forEach function is so slow.
Can anybody explain this? Thank you.

Jakob Kummerow

unread,
May 23, 2013, 4:51:21 AM5/23/13
to v8-u...@googlegroups.com
Here's what the builtin forEach function does: https://code.google.com/p/v8/source/browse/branches/bleeding_edge/src/array.js#1078

Notably, for every index it checks if the array actually contains that index. This check is mandated by the ECMAScript specification, see http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf chapter 15.4.4.18.

--
--
v8-users mailing list
v8-u...@googlegroups.com
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 v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Sven Panne

unread,
May 23, 2013, 4:57:05 AM5/23/13
to v8-u...@googlegroups.com
Further note: When your test on IE10 shows that it is almost twice as fast as the plain old for loop, check if your test contains dead code. IE10 is very good at removing this, it easily gives you good benchmark scores without actually doing anything fantastic, because a lot of benchmarks contain lots of dead code. Well-known compiler writer trick for decades... ;-)

刘玉龙

unread,
May 23, 2013, 8:24:38 AM5/23/13
to v8-u...@googlegroups.com
OK. I will remember this :)

Anyway, it's not bad if compiler can auto remove the dead code~

在 2013年5月23日星期四UTC+8下午4时57分05秒,Sven Panne写道:

Kevin Millikin

unread,
May 23, 2013, 9:51:16 AM5/23/13
to v8-u...@googlegroups.com
Real applications don't normally contain expensive dead code (what programmer would waste their time writing that?).  If you find that your application does contain such code, there is an easy workaround.


--

刘玉龙

unread,
May 23, 2013, 9:53:07 PM5/23/13
to v8-u...@googlegroups.com
Hey, I have another question.

I noticed that jquery, underscore and some other libraries, don't check if the index is in array. Under what circumstances this lacking of check will cause a bug? Thank you.

在 2013年5月23日星期四UTC+8下午4时51分21秒,Jakob Kummerow写道:

Jakob Kummerow

unread,
May 24, 2013, 4:51:54 AM5/24/13
to v8-u...@googlegroups.com
On Fri, May 24, 2013 at 3:53 AM, 刘玉龙 <lyl8...@gmail.com> wrote:
Hey, I have another question.

I noticed that jquery, underscore and some other libraries, don't check if the index is in array. Under what circumstances this lacking of check will cause a bug?

That depends on your definition of "bug"; or, put differently: on your program's expectations how missing elements should be treated when walking over an array. Consider this example:

> var a = [1,,,4]
> a.forEach(function(x) { console.log(x);})
1
4
> for (var i = 0; i < a.length; ++i) console.log(a[i])
1
undefined
undefined
4
Reply all
Reply to author
Forward
0 new messages