Eval+Join vs Reduce

JavaScript performance comparison

Test case created by Joshua T Kalis

Info

I know eval is evil but how does it perform next to reduce in this particular instance?

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var sm = [10, 23, 41, 13],
        md = [1, 7, 13, 22, 36, 42, 55, 60, 72, 88, 90, 103, 123, 158, 200, 222, 323, 443, 501, 545, 610, 728, 880, 891, 903, 919, 1001, 2011]
        lg = (function() {
          var result = [];
          for (var i = 0; i < 200; i++) {
            result.push(~~ (Math.random() * 1000));
          }
          return result;
        }());
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
eval+join
eval(sm.join("+"));
eval(md.join("+"));
eval(lg.join("+"));
pending…
reduce
sm.reduce(function (a, b) { return a + b; });
md.reduce(function (a, b) { return a + b; });
lg.reduce(function (a, b) { return a + b; });
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment