Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Practically Immutable
Search
sporto
March 28, 2015
Technology
0
190
Practically Immutable
Immutable data in practice in JavaScript
sporto
March 28, 2015
Tweet
Share
More Decks by sporto
See All by sporto
React inside Elm
sporto
2
180
Elm
sporto
1
260
Redux: Flux Reduced
sporto
1
350
Webpack and React
sporto
4
390
Rails with Webpack
sporto
1
220
Lesson learnt building Single Page Application
sporto
0
130
Grunt
sporto
1
180
Safe Testing in Ruby
sporto
1
130
Go - A great language for building web applications
sporto
1
340
Other Decks in Technology
See All in Technology
リアーキテクティングのその先へ 〜品質と開発生産性の壁を越えるプラットフォーム戦略〜 / architecture-con2025
visional_engineering_and_design
0
4.8k
レガシーシステム刷新における TypeSpec スキーマ駆動開発のすゝめ
tsukuha
1
480
AWS re:Invent 2025 で頻出の 生成 AI サービスをおさらい
komakichi
2
180
国産クラウドを支える設計とチームの変遷 “技術・組織・ミッション”
kazeburo
4
6.6k
マルチドライブアーキテクチャ: 複数の駆動力でプロダクトを前進させる
knih
0
7.8k
Rubyist入門: The Way to The Timeless Way of Programming
snoozer05
PRO
7
540
OSだってコンテナしたい❗Image Modeが切り拓くLinux OS運用の新時代
tsukaman
0
120
2025年 面白の現在地 / Where Omoshiro Stands Today: 2025
acidlemon
0
140
持続可能なアクセシビリティ開発
azukiazusa1
6
290
今すぐGoogle Antigravityを触りましょう
rfdnxbro
0
100
Perlの生きのこり - YAPC::Fukuoka 2025
kfly8
0
740
SRE視点で振り返るメルカリのアーキテクチャ変遷と普遍的な考え
foostan
2
440
Featured
See All Featured
Learning to Love Humans: Emotional Interface Design
aarron
274
41k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.1k
Bash Introduction
62gerente
615
210k
Imperfection Machines: The Place of Print at Facebook
scottboms
269
13k
Writing Fast Ruby
sferik
630
62k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
We Have a Design System, Now What?
morganepeng
54
7.9k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
11
940
Thoughts on Productivity
jonyablonski
73
4.9k
A Tale of Four Properties
chriscoyier
162
23k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.3k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Transcript
practically immutable ! Immutable data in JS @sebasporto
I build CRUD apps, mostly
What? • Something that cannot change • Once created stays
the same
var array1 = [1, 2].immutable var array2 = array1.push(3) !
array1 //=> [1, 2] array2 //=> [1, 2, 3]
WHY?
# 1 no side effects
var orders = [1, 2]; processOrders(orders); what is orders after
this?
Predictability Store Comp Comp Data Data wants to sort data
wants to transform data doesn't want its data to be modified externally
Enforces uni directional data flow React + Flux Store Comp
Action
# 2 Easier workflows
Undo add(item) { history.push(items) items = items.push(item) } undo() {
items = history.pop() }
Dirty objects Get record from store Clone record Edit record
Save Replace original Yes Discard No
Dirty objects Get record from store Edit record Save Replace
original Yes Discard No
HOW?
clojurescript immutable data for free but this talk is about
plain JS
• In JS strings and numbers are immutable (not the
case in every language) • Arrays, objects are not - so we need help here • JS and immutable data => opposing forces
immutable.js • Great for simple collections • [1, 2, ...]
['a', 'b' ...] • Caveats for collections with objects https://github.com/facebook/immutable-js
Immutable.js var record = {label: 'Sam'}; var list = Immutable.List([record]);
! console.log(list.get(0).label) // => 'Sam' http://jsbin.com/dilupu/1/embed?js,console record.label = 'Biff' ! console.log(list.get(0).label) // => 'Biff' Opps!
immutable.js To be fair: Immutable.js can do the right thing
if you learn it really well Immutable.js != Just works™
Seamless immutable • Fixes Immutable.js issues with deep objects https://github.com/rtfeldman/seamless-immutable
var record = {label: 'Sam'}; var list = Immutable([record]); !
console.log(list[0].label) // => 'Sam' ! record.label = 'Biff' ! console.log(list[0].label) // => 'Sam' http://jsbin.com/fayupu/1/edit?js,console Seamless immutable
imm https://github.com/sporto/imm • Build on top of seamless-immutable • Nice
CRUD API
imm list = list.add(record) list = list.replace(record) list = list.remove(id)
! record = list.get(id)
in Conclusion
Immutable data can make your application more robust
but play a lot with your chosen lib, understand
the caveats
Thanks