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
200
0
Share
Practically Immutable
Immutable data in practice in JavaScript
sporto
March 28, 2015
More Decks by sporto
See All by sporto
React inside Elm
sporto
2
190
Elm
sporto
1
280
Redux: Flux Reduced
sporto
1
370
Webpack and React
sporto
4
400
Rails with Webpack
sporto
1
240
Lesson learnt building Single Page Application
sporto
0
140
Grunt
sporto
1
200
Safe Testing in Ruby
sporto
1
140
Go - A great language for building web applications
sporto
1
350
Other Decks in Technology
See All in Technology
AIと乗り切った1,500ページ超のヘルプサイト基盤刷新とさらにその先の話
mugi_uno
2
310
GKE Agent SandboxでAIが生成したコードを 安全に実行してみた
lamaglama39
0
210
サンプリングは「作る」のか「使う」のか? 分散トレースのコストと運用を両立する実践的戦略 / Why you need the tail sampling and why you don't want it
ymotongpoo
3
120
AIエージェントの支払い基盤 AgentCore Payments概要
kmiya84377
1
130
【技術書典20】OpenFOAM(自宅で深める流体解析)流れと熱移動(2)
kamakiri1225
0
380
Forget technical debt
ufried
0
170
[Oracle TechNight#99] 生成AI時代のAI/ML入門 ~ AIとオラクルデータベースの関係 (後半)
oracle4engineer
PRO
3
240
VespaのParent Childを用いたフィードパフォーマンスの改善
taking
0
270
アクセシビリティはすべての人のもの
tomokusaba
0
290
AI 時代の Platform Engineering
recruitengineers
PRO
1
100
試作とデモンストレーション / Prototyping and Demonstrations
ks91
PRO
0
190
雑談は、センサーだった
bitkey
PRO
2
210
Featured
See All Featured
The agentic SEO stack - context over prompts
schlessera
0
770
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
240
Embracing the Ebb and Flow
colly
88
5k
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
500
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.8k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
560
Optimising Largest Contentful Paint
csswizardry
37
3.7k
How to Talk to Developers About Accessibility
jct
2
190
The Mindset for Success: Future Career Progression
greggifford
PRO
0
320
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
130
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
Navigating Team Friction
lara
192
16k
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