foldl vs foldr
JavaScript performance comparison
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.
Test | Ops/sec | |
---|---|---|
foldr
|
|
pending… |
foldl
|
|
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.
- Revision 1: published jrvieira
- Revision 2: published José Rafael Vieira
- Revision 3: published José Rafael Vieira
- Revision 4: published José Rafael Vieira
0 Comments