imperative-vs-fuctional-code-test

JavaScript performance comparison

Test case created by XLee

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var cats = [
    { name: 'Mojo',    months: 84 },
    { name: 'Mao-Mao', months: 34 },
    { name: 'Waffles', months: 4 },
    { name: 'Pickles', months: 6 }
  ];
  var isKitten = function(cat){return cat.months < 7};
  var getName = function(cat) {return cat.name};
  
  

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
functional
var kittens =
  cats.filter(isKitten)
      .map(getName)
pending…
imperative completely
var kittens = []
// typical poorly written `for loop`
for (var i = 0; i < cats.length; i++) {
  if (cats[i].months < 7) {
    kittens.push(cats[i].name)
  }
}
pending…
imperative code with function invoked
var kittens = []
for (var i = 0; i < cats.length; i++) {
  if (isKitten(cats[i])) {
    kittens.push(getName(cats[i]))
  }
}
pending…
functional with anonymous function
var kittens =
  cats.filter(function(cat){return cat.months < 7})
      .map(function(cat) {return cat.name})
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

1 Comment

XLee (revision owner) commented :

the conclusion is : there is a negligible distance between pure imperative code and imperative-functions version. there is an obvious diffrence between functional style and the two others.

but, we can NOT overlook the briefness and the intelligibility of functional programing.