foldl vs foldr

JavaScript performance comparison

Test case created by jrvieira

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  ZERO = f => x => x
  succ = n => f => x => f(n(f)(x))
  
  add = n => k => n(succ)(k)
  
  pair = a => b => f => f(a)(b)
  
  head = p => p(a => b => a)
  tail = p => p(a => b => b)
  
  NIL = () => {}
  
  foldr = f => x => l => l === NIL ? x
                                   : f(head(l))(foldr(f)(x)(tail(l)))
  
  foldl = f => x => l => l === NIL ? x
                                   : foldl(f)(f(x)(head(l)))(tail(l))
  
  // generate list
  list = pair(ZERO)(NIL)
  let i = 100
  while (--i) { list = pair(succ(head(list)))(list) }
  
  listn = pair(0)(NIL)
  let ii = 100
  while (--ii) { listn = pair(head(listn)+1)(listn) }

};
</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
foldr
//foldr(add)(ZERO)(list)
foldr(x=>y=>x+y)(0)(listn)
pending…
foldl
//foldl(add)(ZERO)(list)
foldl(x=>y=>x+y)(0)(listn)
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.

0 Comments