Created
August 19, 2014 20:00
-
-
Save ElliotChong/6580a047d235829cf08b to your computer and use it in GitHub Desktop.
Super quick and very dirty memory benchmark of Immutable JS.
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
heapdump = require "heapdump" | |
Immutable = require "immutable" | |
pixels = 256 * 256 | |
iterations = 1000 | |
# Immutable | |
testImmutable = -> | |
# Simulate a pixel grid | |
grid = Immutable.Vector() | |
for [0...pixels] | |
grid = grid.push 0 | |
history = Immutable.Vector grid | |
for i in [0...iterations] | |
grid = grid.set Math.round(Math.random() * pixels), Math.round(Math.random() * 16777215) # FFFFFF | |
history = history.push grid | |
if process.env.HEAP_DUMP?.toString() is "true" | |
heapdump.writeSnapshot() | |
# Vanilla Morph | |
testVanillaMorph = -> | |
grid = for [0...pixels] | |
0 | |
for i in [0...iterations] | |
index = Math.round(Math.random() * pixels) | |
value = Math.round(Math.random() * 16777215) # FFFFFF | |
grid[index] = value | |
if process.env.HEAP_DUMP?.toString() is "true" | |
heapdump.writeSnapshot() | |
# Vanilla Immutable | |
testVanillaImmutable = -> | |
grid = for [0...pixels] | |
0 | |
history = [grid] | |
for i in [0...iterations] | |
index = Math.round(Math.random() * pixels) | |
value = Math.round(Math.random() * 16777215) # FFFFFF | |
grid[index] = value | |
history.push grid.slice() | |
if process.env.HEAP_DUMP?.toString() is "true" | |
heapdump.writeSnapshot() | |
# Vanilla Diff | |
testVanillaDiff = -> | |
grid = for [0...pixels] | |
0 | |
history = [grid] | |
for i in [0...iterations] | |
index = Math.round(Math.random() * pixels) | |
value = Math.round(Math.random() * 16777215) # FFFFFF | |
grid[index] = value | |
history.push index: index, value: value | |
if process.env.HEAP_DUMP?.toString() is "true" | |
heapdump.writeSnapshot() | |
if process.env.VANILLA_MORPH?.toString() is "true" | |
testVanillaMorph() | |
if process.env.VANILLA_IMMUTABLE?.toString() is "true" | |
testVanillaImmutable() | |
if process.env.VANILLA_DIFF?.toString() is "true" | |
testVanillaDiff() | |
if process.env.IMMUTABLE?.toString() is "true" | |
testImmutable() | |
module.exports = immutable: testImmutable, vanillaDiff: testVanillaDiff, vanillaImmutable: testVanillaImmutable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment