SlideShare a Scribd company logo
Immutable Libraries
for React
Simon Bächler, ti&m
Simon Bächler
ti&m
@simonbaechler
The Flux Pattern
Redux
Immutable state
Redux requires
• Immutable objects for state
• Pure functions for reducers and selectors.
With mutations
case ADD_VIDEO_TO_SERVICE:
state.profile.accounts[action.serviceId]
.videos[action.videoInfo.id] = action.videoInfo;
return state;
With Immer
case ADD_VIDEO_TO_SERVICE:
return produce(state, draft => {
draft.profile.accounts[action.serviceId]
.videos[action.videoInfo.id] = action.videoInfo;
});
Immer
• By Michel Weststrate (Released January 2018)
• Producer function: mutable copy of the state.
• Simple API: Only one function:
produce<S>(currentState: S, draft: (S) => void): S
• Partial application is possible (by providing a function as the
first argument).
• Auto Freezing in Dev mode.
• Requires a Proxy-Polyfill für IE11.
With Immutable.js
case ADD_VIDEO_TO_SERVICE:
return state.setIn(
['profile', 'accounts', action.serviceId, 'videos', action.videoInfo.id],
action.videoInfo
);
Immutable.js
• By Facebook
• Uses a tree data type: Well suited for very large states.
• Comes with its own API. e.g. getters for accessing properties.
• Many helper methods for the Collection type.
• Lazy sequences and Records
• Requires a browser extension for debugging.
• The main developer has left Facebook.
• Immutable structures should not be mixed with JS structures
• toJS() is an expensive operation.
Tries
• Prefix tree
• The path is the key
• The leaf is the value
Updating a value
• Change the value of
'tea'.
Updating a value
• A new tree is created
(clone the root node)
• Only the leaves in the
path are re-created
• The old tree is still
there as long as a
reference is pointing
at it.
https://medium.com/@dtinth/immutable-js-persistent-data-structures-and-structural-sharing-6d163fbd73d2
Code and
Performance
Adding nodes
state => {
for (let i = 0; i < 1000; i++) {
state = {
ids: [...state.ids, i],
map: {
...state.map,
[i]: {
a: 1,
b: "Some data here"
}
}
}
}
}
Adding nodes
state => {
for (let i = 0; i < 1000; i++) {
state = state.withMutations(draft => {
draft.update("ids", ids => ids.push(i))
draft.setIn(["map", i], Immutable.Map({
a: 1,
b: "Some data here"
}))
})
}
}
Adding nodes
state => {
for (let i = 0; i < 1000; i++) {
state = produce(state, draft => {
draft.ids.push(i)
draft.map[i] = {
a: 1,
b: "Some data here"
}
})
}
}
Update lists
0
40
80
120
160
JS ImmutableJS Immer
t(ms)
Update lists
0
300
600
900
1200
1500
JS ImmutableJS Immer Immer ES5
t(ms)
Update a tree (1.2M nodes)
Update a tree (1.2M nodes)
state => {
for (let i = 0; i < 1000; i++) {
k = getKey(i);
state = {
...state,
[k]: {
...state[k],
[k]: {
...state[k][k],
[k]: {
...state[k][k][k],
[k]: {
...state[k][k][k][k],
[k]: {
...state[k][k][k][k][k],
[k]: {
...state[k][k][k][k][k][k],
[k]: i
}
}
}
}
}
}
}
}
Update a tree (1.2M nodes)
state => {
for (let i = 0; i < 1000; i++) {
k = getKey(i);
state = state.setIn([k, k, k, k, k, k, k], i)
}
}
Update a tree (1.2M nodes)
state => {
for (let i = 0; i < 1000; i++) {
k = getKey(i);
state = produce(state, draft => {
draft[k][k][k][k][k][k][k] = i
})
}
}
Update a tree (1.2M nodes)
0
7.5
15
22.5
30
JS ImmutableJS Immer
t(ms)
Update a tree (1.2M nodes)
0
22.5
45
67.5
90
JS ImmutableJS Immer Immer ES5
t(ms)
Update a tree (1.2M nodes)
state => {
for (let i = 0; i < 1000; i++) {
selectId = i % childrenCount
state = {
...state,
children: state.children.map((n, j) => {
if (j !== selectId) {
return n
}
return {
...n,
children: n.children.map((n, j) => {
if (j !== selectId) {
return n
}
return {
...n,
children: n.children.map((n, j) => {
if (j !== selectId) {
return n
}
return {
...n,
children: n.children.map((n, j) => {
if (j !== selectId) {
return n
}
return {
...n,
children: n.children.map(
(n, j) => {
if (j !== selectId) {
return n
}
return {
...n,
children: n.children.map(
(n, j) => {
if (
j !==
selectId
) {
return n
}
return {
...n,
value:
n.value +
1
Update a tree (1.2M nodes)
state => {
for (let i = 0; i < 1000; i++) {
selectId = i % childrenCount
state = state.updateIn(
[
"children",
selectId,
"children",
selectId,
"children",
selectId,
"children",
selectId,
"children",
selectId,
"children",
selectId,
"value"
],
value => value + 1
)
}
}
Update a tree (1.2M nodes)
state => {
for (let i = 0; i < 1000; i++) {
selectId = i % childrenCount
state = produce(state, draft => {
draft
.children[selectId].children[selectId]
.children[selectId].children[selectId]
.children[selectId].children[selectId]
.value += 1
})
}
}
Update a tree (1.2M nodes)
0
5
10
15
20
25
30
35
40
JS ImmutableJS Immer
t(ms)
Update a tree (1.2M nodes)
0
30
60
90
120
150
JS ImmutableJS Immer Immer ES5
t(ms)
Summary
• Use a helper library for updating immutable state.
• Native JS is really fast on small and nested structures.
• Immutable.js shines on large, flat data structures but
comes with some downsides (API, overhead, deep
integration)
• Immer makes your code more readable and is about 2x
slower in ES6, but massively slower when transpiled to
ES5.

More Related Content

What's hot

Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSONFun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
Tomomi Imura
 
GraphQL: APIs the New Way.
GraphQL: APIs the New Way.GraphQL: APIs the New Way.
GraphQL: APIs the New Way.
EatDog
 
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
Arpita Patel
 
Sql server ___________session_20(ddl triggers)
Sql server  ___________session_20(ddl triggers)Sql server  ___________session_20(ddl triggers)
Sql server ___________session_20(ddl triggers)
Ehtisham Ali
 
Taming the browser with the YUI Dom Component
Taming the browser with the YUI Dom ComponentTaming the browser with the YUI Dom Component
Taming the browser with the YUI Dom Component
Christian Heilmann
 
Machine Learning on GCP
Machine Learning on GCPMachine Learning on GCP
Machine Learning on GCP
AllCloud
 
High performance GPU computing with Ruby
High performance GPU computing with RubyHigh performance GPU computing with Ruby
High performance GPU computing with Ruby
Prasun Anand
 
Luca Passani - Essential Tools for Mobile-Aware Web Professionals | Codemoti...
Luca Passani - Essential Tools for Mobile-Aware Web Professionals |  Codemoti...Luca Passani - Essential Tools for Mobile-Aware Web Professionals |  Codemoti...
Luca Passani - Essential Tools for Mobile-Aware Web Professionals | Codemoti...
Codemotion
 
Forecast stock prices python
Forecast stock prices pythonForecast stock prices python
Forecast stock prices python
Utkarsh Asthana
 
Reactive data visualisations with Om
Reactive data visualisations with OmReactive data visualisations with Om
Reactive data visualisations with Om
Anna Pawlicka
 
pewPew<s>xxd
pewPew<s>xxdpewPew<s>xxd
pewPew<s>xxd
SomeRandoDudewhojust
 
Functional Programming from OO perspective (Sayeret Lambda lecture)
Functional Programming from OO perspective (Sayeret Lambda lecture)Functional Programming from OO perspective (Sayeret Lambda lecture)
Functional Programming from OO perspective (Sayeret Lambda lecture)
Ittay Dror
 
Vancouver Canada MuleSoft Meetup Nov 2020
Vancouver Canada MuleSoft Meetup Nov 2020Vancouver Canada MuleSoft Meetup Nov 2020
Vancouver Canada MuleSoft Meetup Nov 2020
Yashwant Palkar
 
Classing up ES6 - Web Directions code 2015 (1)
Classing up ES6 - Web Directions code 2015 (1)Classing up ES6 - Web Directions code 2015 (1)
Classing up ES6 - Web Directions code 2015 (1)Andy Sharman
 
Kotlin Mullets
Kotlin MulletsKotlin Mullets
Kotlin Mullets
James Ward
 
addition of two matrices
addition of two matricesaddition of two matrices
addition of two matrices
A A
 
The Future of Qt Widgets
The Future of Qt WidgetsThe Future of Qt Widgets
The Future of Qt Widgets
Marius Bugge Monsen
 
Java card and flow layout
Java card and flow layoutJava card and flow layout
Java card and flow layout
shiprashakya2
 
The Next Generation Qt Item Views
The Next Generation Qt Item ViewsThe Next Generation Qt Item Views
The Next Generation Qt Item Views
account inactive
 
Combined presentation init
Combined presentation initCombined presentation init
Combined presentation initkhalid mansoor
 

What's hot (20)

Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSONFun with D3.js: Data Visualization Eye Candy with Streaming JSON
Fun with D3.js: Data Visualization Eye Candy with Streaming JSON
 
GraphQL: APIs the New Way.
GraphQL: APIs the New Way.GraphQL: APIs the New Way.
GraphQL: APIs the New Way.
 
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
 
Sql server ___________session_20(ddl triggers)
Sql server  ___________session_20(ddl triggers)Sql server  ___________session_20(ddl triggers)
Sql server ___________session_20(ddl triggers)
 
Taming the browser with the YUI Dom Component
Taming the browser with the YUI Dom ComponentTaming the browser with the YUI Dom Component
Taming the browser with the YUI Dom Component
 
Machine Learning on GCP
Machine Learning on GCPMachine Learning on GCP
Machine Learning on GCP
 
High performance GPU computing with Ruby
High performance GPU computing with RubyHigh performance GPU computing with Ruby
High performance GPU computing with Ruby
 
Luca Passani - Essential Tools for Mobile-Aware Web Professionals | Codemoti...
Luca Passani - Essential Tools for Mobile-Aware Web Professionals |  Codemoti...Luca Passani - Essential Tools for Mobile-Aware Web Professionals |  Codemoti...
Luca Passani - Essential Tools for Mobile-Aware Web Professionals | Codemoti...
 
Forecast stock prices python
Forecast stock prices pythonForecast stock prices python
Forecast stock prices python
 
Reactive data visualisations with Om
Reactive data visualisations with OmReactive data visualisations with Om
Reactive data visualisations with Om
 
pewPew<s>xxd
pewPew<s>xxdpewPew<s>xxd
pewPew<s>xxd
 
Functional Programming from OO perspective (Sayeret Lambda lecture)
Functional Programming from OO perspective (Sayeret Lambda lecture)Functional Programming from OO perspective (Sayeret Lambda lecture)
Functional Programming from OO perspective (Sayeret Lambda lecture)
 
Vancouver Canada MuleSoft Meetup Nov 2020
Vancouver Canada MuleSoft Meetup Nov 2020Vancouver Canada MuleSoft Meetup Nov 2020
Vancouver Canada MuleSoft Meetup Nov 2020
 
Classing up ES6 - Web Directions code 2015 (1)
Classing up ES6 - Web Directions code 2015 (1)Classing up ES6 - Web Directions code 2015 (1)
Classing up ES6 - Web Directions code 2015 (1)
 
Kotlin Mullets
Kotlin MulletsKotlin Mullets
Kotlin Mullets
 
addition of two matrices
addition of two matricesaddition of two matrices
addition of two matrices
 
The Future of Qt Widgets
The Future of Qt WidgetsThe Future of Qt Widgets
The Future of Qt Widgets
 
Java card and flow layout
Java card and flow layoutJava card and flow layout
Java card and flow layout
 
The Next Generation Qt Item Views
The Next Generation Qt Item ViewsThe Next Generation Qt Item Views
The Next Generation Qt Item Views
 
Combined presentation init
Combined presentation initCombined presentation init
Combined presentation init
 

Similar to Immutable Libraries for React

PyCon SG x Jublia - Building a simple-to-use Database Management tool
PyCon SG x Jublia - Building a simple-to-use Database Management toolPyCon SG x Jublia - Building a simple-to-use Database Management tool
PyCon SG x Jublia - Building a simple-to-use Database Management tool
Crea Very
 
Vuexと入力フォーム
Vuexと入力フォームVuexと入力フォーム
Vuexと入力フォーム
Joe_noh
 
Redux "Bad" Practices - A List of 13 Bad Practices and How to Avoid Them
Redux "Bad" Practices - A List of 13 Bad Practices and How to Avoid ThemRedux "Bad" Practices - A List of 13 Bad Practices and How to Avoid Them
Redux "Bad" Practices - A List of 13 Bad Practices and How to Avoid Them
Adam Klein
 
Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)
Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)
Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)
indeedeng
 
첫 리액트 경험기
첫 리액트 경험기첫 리액트 경험기
첫 리액트 경험기
석진 고
 
Better Together: Building Scalable Real Time Collaborative Apps with Node.js
Better Together: Building Scalable Real Time Collaborative Apps with Node.js  Better Together: Building Scalable Real Time Collaborative Apps with Node.js
Better Together: Building Scalable Real Time Collaborative Apps with Node.js
Kav Latiolais
 
Func up your code
Func up your codeFunc up your code
Func up your code
Maciej Komorowski
 
Crossing platforms with JavaScript & React
Crossing platforms with JavaScript & React Crossing platforms with JavaScript & React
Crossing platforms with JavaScript & React
Robert DeLuca
 
Compose Async with RxJS
Compose Async with RxJSCompose Async with RxJS
Compose Async with RxJS
Kyung Yeol Kim
 
State managment in a world of hooks
State managment in a world of hooksState managment in a world of hooks
State managment in a world of hooks
500Tech
 
AWS IoTで家庭内IoTをやってみた【JAWS DAYS 2016】
AWS IoTで家庭内IoTをやってみた【JAWS DAYS 2016】AWS IoTで家庭内IoTをやってみた【JAWS DAYS 2016】
AWS IoTで家庭内IoTをやってみた【JAWS DAYS 2016】
tsuchimon
 
React new features and intro to Hooks
React new features and intro to HooksReact new features and intro to Hooks
React new features and intro to Hooks
Soluto
 
Taming forms with React
Taming forms with ReactTaming forms with React
Taming forms with React
GreeceJS
 
Managing Complex UI using xState
Managing Complex UI using xStateManaging Complex UI using xState
Managing Complex UI using xState
Xavier Lozinguez
 
Michael Hackstein - NoSQL meets Microservices - NoSQL matters Dublin 2015
Michael Hackstein - NoSQL meets Microservices - NoSQL matters Dublin 2015Michael Hackstein - NoSQL meets Microservices - NoSQL matters Dublin 2015
Michael Hackstein - NoSQL meets Microservices - NoSQL matters Dublin 2015
NoSQLmatters
 
NoSQL meets Microservices
NoSQL meets MicroservicesNoSQL meets Microservices
NoSQL meets Microservices
ArangoDB Database
 
Introduction to Redux
Introduction to ReduxIntroduction to Redux
Introduction to Redux
Ignacio Martín
 
Data in Motion: Streaming Static Data Efficiently
Data in Motion: Streaming Static Data EfficientlyData in Motion: Streaming Static Data Efficiently
Data in Motion: Streaming Static Data Efficiently
Martin Zapletal
 
GHC Participant Training
GHC Participant TrainingGHC Participant Training
GHC Participant TrainingAidIQ
 
Svcc 2013-d3
Svcc 2013-d3Svcc 2013-d3
Svcc 2013-d3
Oswald Campesato
 

Similar to Immutable Libraries for React (20)

PyCon SG x Jublia - Building a simple-to-use Database Management tool
PyCon SG x Jublia - Building a simple-to-use Database Management toolPyCon SG x Jublia - Building a simple-to-use Database Management tool
PyCon SG x Jublia - Building a simple-to-use Database Management tool
 
Vuexと入力フォーム
Vuexと入力フォームVuexと入力フォーム
Vuexと入力フォーム
 
Redux "Bad" Practices - A List of 13 Bad Practices and How to Avoid Them
Redux "Bad" Practices - A List of 13 Bad Practices and How to Avoid ThemRedux "Bad" Practices - A List of 13 Bad Practices and How to Avoid Them
Redux "Bad" Practices - A List of 13 Bad Practices and How to Avoid Them
 
Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)
Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)
Indeed My Jobs: A case study in ReactJS and Redux (Meetup talk March 2016)
 
첫 리액트 경험기
첫 리액트 경험기첫 리액트 경험기
첫 리액트 경험기
 
Better Together: Building Scalable Real Time Collaborative Apps with Node.js
Better Together: Building Scalable Real Time Collaborative Apps with Node.js  Better Together: Building Scalable Real Time Collaborative Apps with Node.js
Better Together: Building Scalable Real Time Collaborative Apps with Node.js
 
Func up your code
Func up your codeFunc up your code
Func up your code
 
Crossing platforms with JavaScript & React
Crossing platforms with JavaScript & React Crossing platforms with JavaScript & React
Crossing platforms with JavaScript & React
 
Compose Async with RxJS
Compose Async with RxJSCompose Async with RxJS
Compose Async with RxJS
 
State managment in a world of hooks
State managment in a world of hooksState managment in a world of hooks
State managment in a world of hooks
 
AWS IoTで家庭内IoTをやってみた【JAWS DAYS 2016】
AWS IoTで家庭内IoTをやってみた【JAWS DAYS 2016】AWS IoTで家庭内IoTをやってみた【JAWS DAYS 2016】
AWS IoTで家庭内IoTをやってみた【JAWS DAYS 2016】
 
React new features and intro to Hooks
React new features and intro to HooksReact new features and intro to Hooks
React new features and intro to Hooks
 
Taming forms with React
Taming forms with ReactTaming forms with React
Taming forms with React
 
Managing Complex UI using xState
Managing Complex UI using xStateManaging Complex UI using xState
Managing Complex UI using xState
 
Michael Hackstein - NoSQL meets Microservices - NoSQL matters Dublin 2015
Michael Hackstein - NoSQL meets Microservices - NoSQL matters Dublin 2015Michael Hackstein - NoSQL meets Microservices - NoSQL matters Dublin 2015
Michael Hackstein - NoSQL meets Microservices - NoSQL matters Dublin 2015
 
NoSQL meets Microservices
NoSQL meets MicroservicesNoSQL meets Microservices
NoSQL meets Microservices
 
Introduction to Redux
Introduction to ReduxIntroduction to Redux
Introduction to Redux
 
Data in Motion: Streaming Static Data Efficiently
Data in Motion: Streaming Static Data EfficientlyData in Motion: Streaming Static Data Efficiently
Data in Motion: Streaming Static Data Efficiently
 
GHC Participant Training
GHC Participant TrainingGHC Participant Training
GHC Participant Training
 
Svcc 2013-d3
Svcc 2013-d3Svcc 2013-d3
Svcc 2013-d3
 

More from stbaechler

Distributed apps
Distributed appsDistributed apps
Distributed apps
stbaechler
 
Javascript Bundling and modularization
Javascript Bundling and modularizationJavascript Bundling and modularization
Javascript Bundling and modularization
stbaechler
 
Testing React Applications
Testing React ApplicationsTesting React Applications
Testing React Applications
stbaechler
 
User stories schreiben
User stories schreibenUser stories schreiben
User stories schreiben
stbaechler
 
Nested sets
Nested setsNested sets
Nested sets
stbaechler
 
Microformats
MicroformatsMicroformats
Microformats
stbaechler
 
Zeitplanung mit PERT
Zeitplanung mit PERTZeitplanung mit PERT
Zeitplanung mit PERT
stbaechler
 
Bower Paketmanager
Bower PaketmanagerBower Paketmanager
Bower Paketmanager
stbaechler
 

More from stbaechler (8)

Distributed apps
Distributed appsDistributed apps
Distributed apps
 
Javascript Bundling and modularization
Javascript Bundling and modularizationJavascript Bundling and modularization
Javascript Bundling and modularization
 
Testing React Applications
Testing React ApplicationsTesting React Applications
Testing React Applications
 
User stories schreiben
User stories schreibenUser stories schreiben
User stories schreiben
 
Nested sets
Nested setsNested sets
Nested sets
 
Microformats
MicroformatsMicroformats
Microformats
 
Zeitplanung mit PERT
Zeitplanung mit PERTZeitplanung mit PERT
Zeitplanung mit PERT
 
Bower Paketmanager
Bower PaketmanagerBower Paketmanager
Bower Paketmanager
 

Recently uploaded

Launch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in MinutesLaunch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in Minutes
Roshan Dwivedi
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
QuickwayInfoSystems3
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi ArabiaTop 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Yara Milbes
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
abdulrafaychaudhry
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
abdulrafaychaudhry
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
Boni García
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 

Recently uploaded (20)

Launch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in MinutesLaunch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in Minutes
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi ArabiaTop 7 Unique WhatsApp API Benefits | Saudi Arabia
Top 7 Unique WhatsApp API Benefits | Saudi Arabia
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 

Immutable Libraries for React

  • 4.
  • 6. Redux requires • Immutable objects for state • Pure functions for reducers and selectors.
  • 7.
  • 9. With Immer case ADD_VIDEO_TO_SERVICE: return produce(state, draft => { draft.profile.accounts[action.serviceId] .videos[action.videoInfo.id] = action.videoInfo; });
  • 10. Immer • By Michel Weststrate (Released January 2018) • Producer function: mutable copy of the state. • Simple API: Only one function: produce<S>(currentState: S, draft: (S) => void): S • Partial application is possible (by providing a function as the first argument). • Auto Freezing in Dev mode. • Requires a Proxy-Polyfill für IE11.
  • 11.
  • 12. With Immutable.js case ADD_VIDEO_TO_SERVICE: return state.setIn( ['profile', 'accounts', action.serviceId, 'videos', action.videoInfo.id], action.videoInfo );
  • 13. Immutable.js • By Facebook • Uses a tree data type: Well suited for very large states. • Comes with its own API. e.g. getters for accessing properties. • Many helper methods for the Collection type. • Lazy sequences and Records • Requires a browser extension for debugging. • The main developer has left Facebook. • Immutable structures should not be mixed with JS structures • toJS() is an expensive operation.
  • 14. Tries • Prefix tree • The path is the key • The leaf is the value
  • 15. Updating a value • Change the value of 'tea'.
  • 16. Updating a value • A new tree is created (clone the root node) • Only the leaves in the path are re-created • The old tree is still there as long as a reference is pointing at it.
  • 17.
  • 20. Adding nodes state => { for (let i = 0; i < 1000; i++) { state = { ids: [...state.ids, i], map: { ...state.map, [i]: { a: 1, b: "Some data here" } } } } }
  • 21. Adding nodes state => { for (let i = 0; i < 1000; i++) { state = state.withMutations(draft => { draft.update("ids", ids => ids.push(i)) draft.setIn(["map", i], Immutable.Map({ a: 1, b: "Some data here" })) }) } }
  • 22. Adding nodes state => { for (let i = 0; i < 1000; i++) { state = produce(state, draft => { draft.ids.push(i) draft.map[i] = { a: 1, b: "Some data here" } }) } }
  • 25. Update a tree (1.2M nodes)
  • 26. Update a tree (1.2M nodes) state => { for (let i = 0; i < 1000; i++) { k = getKey(i); state = { ...state, [k]: { ...state[k], [k]: { ...state[k][k], [k]: { ...state[k][k][k], [k]: { ...state[k][k][k][k], [k]: { ...state[k][k][k][k][k], [k]: { ...state[k][k][k][k][k][k], [k]: i } } } } } } } }
  • 27. Update a tree (1.2M nodes) state => { for (let i = 0; i < 1000; i++) { k = getKey(i); state = state.setIn([k, k, k, k, k, k, k], i) } }
  • 28. Update a tree (1.2M nodes) state => { for (let i = 0; i < 1000; i++) { k = getKey(i); state = produce(state, draft => { draft[k][k][k][k][k][k][k] = i }) } }
  • 29. Update a tree (1.2M nodes) 0 7.5 15 22.5 30 JS ImmutableJS Immer t(ms)
  • 30. Update a tree (1.2M nodes) 0 22.5 45 67.5 90 JS ImmutableJS Immer Immer ES5 t(ms)
  • 31. Update a tree (1.2M nodes)
  • 32. state => { for (let i = 0; i < 1000; i++) { selectId = i % childrenCount state = { ...state, children: state.children.map((n, j) => { if (j !== selectId) { return n } return { ...n, children: n.children.map((n, j) => { if (j !== selectId) { return n } return { ...n, children: n.children.map((n, j) => { if (j !== selectId) { return n } return { ...n, children: n.children.map((n, j) => { if (j !== selectId) { return n } return { ...n, children: n.children.map( (n, j) => { if (j !== selectId) { return n } return { ...n, children: n.children.map( (n, j) => { if ( j !== selectId ) { return n } return { ...n, value: n.value + 1
  • 33. Update a tree (1.2M nodes) state => { for (let i = 0; i < 1000; i++) { selectId = i % childrenCount state = state.updateIn( [ "children", selectId, "children", selectId, "children", selectId, "children", selectId, "children", selectId, "children", selectId, "value" ], value => value + 1 ) } }
  • 34. Update a tree (1.2M nodes) state => { for (let i = 0; i < 1000; i++) { selectId = i % childrenCount state = produce(state, draft => { draft .children[selectId].children[selectId] .children[selectId].children[selectId] .children[selectId].children[selectId] .value += 1 }) } }
  • 35. Update a tree (1.2M nodes) 0 5 10 15 20 25 30 35 40 JS ImmutableJS Immer t(ms)
  • 36. Update a tree (1.2M nodes) 0 30 60 90 120 150 JS ImmutableJS Immer Immer ES5 t(ms)
  • 38. • Use a helper library for updating immutable state. • Native JS is really fast on small and nested structures. • Immutable.js shines on large, flat data structures but comes with some downsides (API, overhead, deep integration) • Immer makes your code more readable and is about 2x slower in ES6, but massively slower when transpiled to ES5.

Editor's Notes

  1. Recommended Architecture Pattern for React Apps
  2. Javascript objects are mutable. When you want to update a deeply nested item it gets ugly.
  3. Mutations are not allowed in Redux.
  4. Immer provides a function called produce, which creates a mutable copy of the state.
  5. Immutable provides the function setIn which takes an array of keys for the path (keys can be anything).
  6. Structure of an Immutable Map with 100'000 entries. During an update, only the yellow nodes are re-created. A native Javascript object would copy all properties.
  7. There are different implementations for the trees and nodes. Immutable.js choses the best one transparently depending on the structure of the tree.
  8. Native JS Code State has a list and a map. We add 1000 items to each.
  9. Immutable.js code
  10. Immer code
  11. 106 3 160 1150
  12. 106 3 160 1150
  13. The tree has 6 levels with 9 children per node. It is only made of Javascript objects.
  14. The tree is only made of objects. The native Javascript code.
  15. The immutable.js code.
  16. The immer code.
  17. 4 3 7 90
  18. 4 3 7 90
  19. The tree has 6 levels with 9 children per node. Every node has a 'children' property that contains an array of child nodes.
  20. Javascript code
  21. Immutable.js code. Immutable.js gives you updateIn.
  22. Immer code.
  23. 2 3 20 120
  24. 2 3 20 120