Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[discussion] for loops: benchmarks for io.js wanted #1508

Closed
hellboy81 opened this issue Apr 23, 2015 · 4 comments
Closed

[discussion] for loops: benchmarks for io.js wanted #1508

hellboy81 opened this issue Apr 23, 2015 · 4 comments
Labels
question Issues that look for answers.

Comments

@hellboy81
Copy link

The only benchmarks I found are not up-do-date

V8 (Chrome, Node)
Array: for loop      :: 64 +/- 1%
Array: reverse while :: 60 +/- 0%
Array: forEach()     :: 235 +/- 0%

Should in io.js for Array-s also be used for loop instead of forEach?

@Fishrock123 Fishrock123 added the question Issues that look for answers. label Apr 23, 2015
@Fishrock123
Copy link
Contributor

@hellboy81 these apply to all javascript runtimes.

forEach() is always usually (see below) slower than inline? loops. (for & reverse while)

@petkaantonov
Copy link
Contributor

forEach() is always slower than inline? loops. (for & reverse while)

Under very specific conditions it could theoretically be almost equally fast 😃

@mscdex
Copy link
Contributor

mscdex commented Apr 23, 2015

This jsperf (and its various revisions) has some interesting results regarding loop performance when visiting array elements (both destructive and not).

@chrisdickinson
Copy link
Contributor

The problem with including this sort of benchmark is that it depends on a variety of factors:

  1. What you're looping over (is it a sparse array? a dense array? a uint8array? a smiarray?)
  2. What happens after the loop (does it have to soft de-opt due to instructions after OSR?)
  3. What's in the body of the loop (can the operations inside the loop be OSR'd? can functions in the body be inlined?)
  4. How is the body of the loop defined? If it's a forEach function, is the function in-situ, or is it hoisted to an outer scope to preclude allocations / share optimizations?

There's no one good answer aside from to benchmark your specific use case. If it comes down to choosing one form over another, in nearly all cases I would suggest avoiding basing the decision on the apparent performance differences, since they're subject to change due to optimizations in new versions of V8. That is to say, if you need to close over iterated values, use .forEach or for-of, if you don't, use for or while.

It's unlikely that we'll include a benchmark for various for-loop forms in this repository, since the data from such a benchmark could be misleading. If you're interested in these sorts of things, though, you might watch @trevnorris' esbench project, since I believe he's attempting collect that sort of perf information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

5 participants