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
200
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
190
Elm
sporto
1
270
Redux: Flux Reduced
sporto
1
370
Webpack and React
sporto
4
390
Rails with Webpack
sporto
1
230
Lesson learnt building Single Page Application
sporto
0
140
Grunt
sporto
1
190
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
社内レビューは機能しているのか
matsuba
0
160
Agent ServerはWeb Serverではない。ADKで考えるAgentOps
akiratameto
0
120
VLAモデル構築のための AIロボット向け模倣学習キット
kmatsuiugo
0
290
新規事業×QAの挑戦:不確実性を乗りこなす!フェーズごとに求められるQAの役割変革
hacomono
PRO
0
120
TypeScript 7.0の現在地と備え方
uhyo
7
1.9k
NewSQL_ ストレージ分離と分散合意を用いたスケーラブルアーキテクチャ
hacomono
PRO
4
410
ReactのdangerouslySetInnerHTMLは“dangerously”だから危険 / Security.any #09 卒業したいセキュリティLT
flatt_security
0
350
夢の無限スパゲッティ製造機 #phperkaigi
o0h
PRO
0
260
生成AIで速度と品質を両立する、QAエンジニア・開発者連携のAI協調型テストプロセス
shota_kusaba
0
240
Go標準パッケージのI/O処理をながめる
matumoto
0
240
AI時代の「本当の」ハイブリッドクラウド — エージェントが実現した、あの頃の夢
ebibibi
0
150
Claude Code 2026年 最新アップデート
oikon48
14
11k
Featured
See All Featured
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
240
Why Our Code Smells
bkeepers
PRO
340
58k
Paper Plane (Part 1)
katiecoart
PRO
0
5.7k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.8k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.5k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
1.9k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Site-Speed That Sticks
csswizardry
13
1.1k
What does AI have to do with Human Rights?
axbom
PRO
1
2k
For a Future-Friendly Web
brad_frost
183
10k
First, design no harm
axbom
PRO
2
1.1k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
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