Skip to content

Instantly share code, notes, and snippets.

@ElliotChong
Created August 19, 2014 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ElliotChong/6580a047d235829cf08b to your computer and use it in GitHub Desktop.
Save ElliotChong/6580a047d235829cf08b to your computer and use it in GitHub Desktop.
Super quick and very dirty memory benchmark of Immutable JS.
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