Skip to content

Instantly share code, notes, and snippets.

@alfonsogarciacaro
Last active July 25, 2016 09:45
Show Gist options
  • Save alfonsogarciacaro/ef6e35de11ec100da3749008bba212ec to your computer and use it in GitHub Desktop.
Save alfonsogarciacaro/ef6e35de11ec100da3749008bba212ec to your computer and use it in GitHub Desktop.
Performance tests between native and Immutable Map
"use strict";
var _fableCore = require("fable-core");
var Immutable = require('immutable');
var count, msg;
// F# code
// let test count =
// let mutable m = Map.empty<int, int>
// for i = 0 to count do
// m <- Map.add i i m
// for i = 0 to count do
// Map.find i m |> ignore
var testFableAdd = function (count) {
var m = _fableCore.Map.create(null, new _fableCore.GenericComparer(function (x, y) {
return x < y ? -1 : x > y ? 1 : 0;
}));
for (var i = 0; i <= count; i++) {
m = _fableCore.Map.add(i, i, m);
}
for (var i = 0; i <= count; i++) {
_fableCore.Map.find(i, m);
}
};
var testImmutableAdd = function(count) {
var m = new Immutable.Map();
for(var i = 0; i < count; ++i) {
m = m.set(i, i);
}
for(var j = 0; j < count; ++j) {
m.get(j);
}
}
// F# code
// let test count =
// let m =
// seq { for i = 0 to count do yield i, i }
// |> Map
// for i = 0 to count do
// Map.find i m |> ignore
var testFableBuildFromSeq = function(count) {
var m = _fableCore.Map.create(_fableCore.Seq.delay(function (unitVar) {
return _fableCore.Seq.map(function (i) {
return [i, i];
}, _fableCore.Seq.range(0, count));
}), new _fableCore.GenericComparer(function (x, y) {
return x < y ? -1 : x > y ? 1 : 0;
}));
for (var i = 0; i <= count; i++) {
_fableCore.Map.find(i, m);
}
};
var testImmutableBuildFromSeq = function(count) {
var m = new Immutable.Map(Immutable.Range(0, count).map(function(x) {
return [x, x];
}));
for(var j = 0; j < count; ++j) {
m.get(j);
}
}
count = 1000;
msg = "Map.add - " + count + " items ";
console.time(msg + '(Fable)');
testFableAdd(count);
console.timeEnd(msg + '(Fable)');
console.time(msg + '(Immutable)');
testImmutableAdd(count);
console.timeEnd(msg + '(Immutable)');
count = 100000;
msg = "Map.add - " + count + " items ";
console.time(msg + '(Fable)');
testFableAdd(count);
console.timeEnd(msg + '(Fable)');
console.time(msg + '(Immutable)');
testImmutableAdd(count);
console.timeEnd(msg + '(Immutable)');
count = 1000000;
msg = "Map.add - " + count + " items ";
console.time(msg + '(Fable)');
testFableAdd(count);
console.timeEnd(msg + '(Fable)');
console.time(msg + '(Immutable)');
testImmutableAdd(count);
console.timeEnd(msg + '(Immutable)');
count = 1000000;
msg = "Map.ofSeq - " + count + " items ";
console.time(msg + '(Fable)');
testFableBuildFromSeq(count);
console.timeEnd(msg + '(Fable)');
console.time(msg + '(Immutable)');
testImmutableBuildFromSeq(count);
console.timeEnd(msg + '(Immutable)');
// Results
// Map.add - 1000 items (Fable): 8.850ms
// Map.add - 1000 items (Immutable): 11.099ms
// Map.add - 100000 items (Fable): 489.479ms
// Map.add - 100000 items (Immutable): 252.457ms
// Map.add - 1000000 items (Fable): 5657.007ms
// Map.add - 1000000 items (Immutable): 3350.691ms
// Map.ofSeq - 1000000 items (Fable): 5496.917ms
// Map.ofSeq - 1000000 items (Immutable): 1097.677ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment