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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
130
Go - A great language for building web applications
sporto
1
350
Other Decks in Technology
See All in Technology
AIエンジニア Devin と歩む、自律型運用プロセスの構築
a2ito
0
640
LLM活用の壁を超える:リクルートR&Dの戦略と打ち手
recruitengineers
PRO
1
220
Databricksアシスタントが自分で考えて動く時代に! エージェントモード体験もくもく会
taka_aki
0
300
Snowflakeデータ基盤で挑むAI活用 〜4年間のDataOpsの基礎をもとに〜
kaz3284
1
330
EMからVPoEを経てCTOへ:マネジメントキャリアパスにおける葛藤と成長
kakehashi
PRO
6
650
ブラックボックス観測に基づくAI支援のプロトコルのリバースエンジニアリングと再現~AIを用いたリバースエンジニアリング~ @ SECCON 14 電脳会議 / Reverse Engineering and Reproduction of an AI-Assisted Protocol Based on Black-Box Observation @ SECCON 14 DENNO-KAIGI
chibiegg
0
130
Introduction to Sansan for Engineers / エンジニア向け会社紹介
sansan33
PRO
6
71k
OSSで構築するIT基盤管理実践事例: NetBox・Snipe-IT・FreeRADIUS+PrivacyIDEA / Practical Case Studies of IT Infrastructure Management Using OSS
nttcom
0
190
DX Improvement at Scale
ntk1000
2
210
Introduction to Sansan, inc / Sansan Global Development Center, Inc.
sansan33
PRO
0
3k
技術的負債の泥沼から組織を救う3つの転換点
nwiizo
5
980
Oracle Cloud Infrastructure:2026年2月度サービス・アップデート
oracle4engineer
PRO
0
200
Featured
See All Featured
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
0
170
Everyday Curiosity
cassininazir
0
150
Tell your own story through comics
letsgokoyo
1
830
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
180
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
130
Done Done
chrislema
186
16k
Balancing Empowerment & Direction
lara
5
930
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
170
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.4k
Unsuck your backbone
ammeep
672
58k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.4k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
68
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