10

I was messing around with the benchmark site jfprefs and created my own benchmark at http://jsperf.com/prefix-or-postfix-increment/9.

The benchmarks are variations of Javascript for loops, using prefix and postfix incrementors and the Crockford jslint style of not using an in place incrementor.

for (var index = 0, len = data.length; index < len; ++index) {
  data[index] = data[index] * 2;
}

for (var index = 0, len = data.length; index < len; index++) {
  data[index] = data[index] * 2;
}

for (var index = 0, len = data.length; index < len; index += 1) {
  data[index] = data[index] * 2;
}

After getting the numbers from a couple of runs of the benchmark, I noticed that Firefox is doing about 15 operations per second on average and Chrome is doing around 300.

benchmark results

I thought JaegerMonkey and v8 were fairly comparable in terms of speed? Are my benchmarks flawed somehow, is Firefox doing some kind of throttling here or is the gap really that large between the performance of the Javascript interpreters?

UPDATE: Thanks to jfriend00, I've concluded the difference in performance is not entirely due to the loop iteration, as seen in this version of the test case. As you can see Firefox is slower, but not as much of a gap as we see in the initial test case.

So why is the statement,

data[index] = data[index] * 2;

So much slower on Firefox?

12
  • 1
    Why would you put a math operation and an array operation inside the for loop? How do you know whether the speed difference is the for loop or the operation inside the loop?
    – jfriend00
    Sep 8, 2012 at 4:23
  • 2
    either way, it is still a huge difference
    – Vic
    Sep 8, 2012 at 4:24
  • jfriend00, you have to put something in the for loop, I imagine the interpreter would possibly optimize out a for loop with nothing in it. Sep 8, 2012 at 4:25
  • @Vic - yeah, but the question is about the for loop and this benchmark isn't accurately comparing for loops.
    – jfriend00
    Sep 8, 2012 at 4:26
  • 1
    Unrelated to the actual question, the JavaScript engine in Firefox is SpiderMonkey (aka TraceMonkey or JägerMonkey), not Rhino. Rhino is a separate Mozilla project written in Java, and the performance is definitely not comparable to V8 or SpiderMonkey (but is improving). Sep 9, 2012 at 0:03

2 Answers 2

8

Arrays are tricky in JavaScript. The way you create them, how you fill them (and with what values) can all affect their performance.

There are two basic implementations that engines use. The simplest, most obvious one is a contiguous block of memory (just like a C array, with some metadata, like the length). It's the fastest way, and ideally the implementation you want in most cases.

The problem is, arrays in JavaScript can grow very large just by assigning to an arbitrary index, leaving "holes". For example, if you have a small array:

var array = [1,2,3];

and you assign a value to a large index:

array[1000000] = 4;

you'll end up with an array like this:

[1, 2, 3, undefined, undefined, undefined, ..., undefined, 4]

To save memory, most runtimes will convert array into a "sparse" array. Basically, a hash table, just like regular JS objects. Once that happens, reading or writing to an index goes from simple pointer arithmetic to a much more complicated algorithm, possibly with dynamic memory allocation.

Of course, different runtimes use different heuristics to decide when to convert from one implementation to another, so in some cases, optimizing for Chrome, for example, can hurt performance in Firefox.

In your case, my best guess is that filling the array backwards is causing Firefox to use a sparse array, making it slower.

-5

I hate to give you such a simple answer, but pretty simply: instruction branching: http://igoro.com/archive/fast-and-slow-if-statements-branch-prediction-in-modern-processors/

From what I get from the benchmark, there's something under the hood in these engines that is giving the instruction prediction features of the processor hell.

2
  • 1
    There are no conditional statements in the loops, this has nothing to do with branch prediction. Sep 8, 2012 at 16:22
  • Oh, okay, so... browsers aren't compiled? What about their engines? I suppose neither of them are written in C++ either.
    – alvonellos
    Sep 8, 2012 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.