Last active
August 29, 2015 14:28
-
-
Save dmitry-vsl/9d41a323206726d1a830 to your computer and use it in GitHub Desktop.
Benchmark for implementing undo/redo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node_modules/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script src='node_modules/immutable/dist/immutable.min.js'></script> | |
<script src='index.js'></script> | |
</head> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var I = Immutable; | |
var Map = I.Map; | |
var List = I.List; | |
var Range = I.Range; | |
var ATTRS_NUM = 10; | |
var SHAPES_NUM = 10; | |
var PAGES_NUM = 20; | |
var ITERATIONS = 1000; | |
var READ_COUNT = 500; | |
function genMap(){ | |
var o = {}; | |
for(var i = 0; i < ATTRS_NUM; i++){ | |
o[i.toString()] = i.toString()+'_value'; | |
} | |
return o; | |
} | |
function genArray(n, fn){ | |
var res = []; | |
for(var i = 0; i < n; i++){ | |
res.push(fn()); | |
} | |
return res; | |
} | |
function genPage(){ | |
var p = genMap(); | |
p.shapes = genArray(SHAPES_NUM, genMap); | |
return p; | |
} | |
function genPages(){ | |
return genArray(PAGES_NUM, genPage); | |
} | |
function print(data){ | |
console.log(JSON.stringify(data, undefined, ' ')); | |
} | |
var seed = 1; | |
function random() { | |
var x = Math.sin(seed++) * 10000; | |
return x - Math.floor(x); | |
} | |
function profile(name, init, serialize, writeOp, readOp){ | |
seed = 1; | |
var stack = []; | |
var data = init(genPages()); | |
console.time(name); | |
for(var i = 0; i < ITERATIONS; i++){ | |
var a = Math.floor((random()*ATTRS_NUM)); | |
var s = Math.floor((random()*SHAPES_NUM)); | |
var p = Math.floor((random()*PAGES_NUM)); | |
stack.push(serialize(data)); | |
data = writeOp(data, p, s, a); | |
for(var r = 0; r < READ_COUNT; r++){ | |
var a = Math.floor((random()*ATTRS_NUM)); | |
var s = Math.floor((random()*SHAPES_NUM)); | |
var p = Math.floor((random()*PAGES_NUM)); | |
readOp(data,a,s,p); | |
} | |
} | |
console.timeEnd(name); | |
return stack; | |
} | |
var immutableResults = profile( | |
'immutable', | |
function(data){return I.fromJS(data)}, | |
function(data){return data}, | |
function(data,p,s,a){ | |
return data.setIn([p, 'shapes', s, a.toString()], 'AAA'); | |
}, | |
function(data,p,s,a){ | |
return data.getIn([p, 'shapes', s, a.toString()]); | |
} | |
); | |
var mutableResults = profile( | |
'mutable', | |
function(data){return data}, | |
function(data){return JSON.stringify(data)}, | |
function(data,p,s,a){ | |
data[p]['shapes'][s][a.toString()] = 'AAA'; | |
return data; | |
}, | |
function(data,p,s,a){ | |
return data[p]['shapes'][s][a.toString()]; | |
} | |
); | |
//ensure results are the same | |
console.log( | |
JSON.stringify(mutableResults.map(JSON.parse)) == | |
JSON.stringify(immutableResults) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment