Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Immutability and JavaScript

Immutability and JavaScript

Prasenjit Sharan
JSFoo 2016, Bangalore

Avatar for Prasenjit Sharan

Prasenjit Sharan

September 16, 2016

Other Decks in Programming

Transcript

  1. “Data dominates. If you've chosen the right data structures and

    organized things well, the algorithms will almost always be self-evident. Data structures, not algorithms, are central to programming.” - Rob Pike
  2. “Much of what makes application development difficult is tracking mutation

    and maintaining state. Developing with immutable data encourages you to think differently about how data flows through your application.” - Immutable.js documentation
  3. • var str = "Strings are immutable in JavaScript."; •

    str [3] = “X”; • str.slice (12, 21); • console.log (str); • // Strings are immutable in JavaScript.
  4. • var map1 = Immutable.Map ({a:1, b:2, c:3}); • var

    map2 = map1.set ('b', 50); • console.log (map1); • // {a:1, b:2, c:3} • console.log (map2); • // {a:1, b:50, c:3}
  5. • var list1 = Immutable.List.of (1, 2); • var list2

    = list1.push (3); • console.log (list1); • // List [1, 2] • console.log (list2); • // List [1, 2, 3]
  6. Equality • // Mutable data • var x = {

    foo: "bar" }; • var y = x; • y.foo = "baz"; • x === y; // true • x.foo; // "baz"
  7. Equality • // Immutable data • var x = Immutable.map

    ({ foo: 'bar'}); • var y = x.set ('foo', 'baz'); • x === y; // false • x.get ('foo'); // “bar”
  8. shouldComponentUpdate () shouldComponentUpdate: function (nextProps, nextState) { return this.props.value !==

    nextProps.value; } Example use-case • this.props.value = { foo: 'bar' } • nextProps.value = { foo: 'bar' }, • The references are different even though the props are same
  9. shouldComponentUpdate () shouldComponentUpdate: function (nextProps, nextState) { return this.props.value !==

    nextProps.value; // true } • this.props.value = { foo: 'bar' } • nextProps.value = { foo: 'bar' }, • The references are different even though the props are same. Example use-case
  10. • var list1 = Immutable.List.of (1, 2); var list2 =

    list1.push (3); console.log (list1); // List [1, 2] console.log (list2); // List [1, 2, 3] • console.log (list1 === list2); • // false
  11. List.prototype.push = function (value) { // 1) make a copy

    var clone = deepCopy (this); // 2) edit the copy clone [clone.length] = value; // 3) return the copy return clone; } via @leeb List.push Implementation
  12. List.prototype.push = function (value) { // 1) make a copy

    var clone = deepCopy (this); // 2) edit the copy clone [clone.length] = value; // 3) return the copy return clone; } via @leeb List.push Implementation
  13. List.prototype.push = function (value) { // 1) make a copy

    var clone = deepCopy (this); // 2) edit the copy clone [clone.length] = value; // 3) return the copy return clone; } via @leeb List.push Implementation
  14. List.prototype.push = function (value) { // 1) make a copy

    var clone = deepCopy (this); // 2) edit the copy clone [clone.length] = value; // 3) return the copy return clone; } via @leeb List.push Implementation
  15. 1 0 Structural Sharing 2 3 4 list1 = [1,

    2, 3, 4] list2 = [0, 1, 2, 3, 4]
  16. 1 0 5 Structural Sharing 2 3 4 list1 =

    [1, 2, 3, 4] list2 = [0, 1, 2, 3, 4] list3 = [5, 2, 3, 4]
  17. 1 2 3 4 0 5 Structural Sharing list1 =

    [1, 2, 3, 4] list2 = [0, 1, 2, 3, 4] list3 = [5, 2, 3, 4] {
  18. 1 0 5 Structural Sharing 0 2 3 4 list1

    = [0, 1, 2, 3, 4] list2 = [0, 5, 2, 3, 4]
  19. 128 129 143 142 141 140 139 138 137 136

    135 134 133 132 131 130 Bitmapped Vector Trie Each node is an array Data lives in the leaves
  20. 128 129 143 142 141 140 139 138 137 136

    135 134 133 132 131 130 list.get (141);
  21. 128 129 143 142 141 140 139 138 137 136

    135 134 133 132 131 130 list.get (0x10001101);
  22. 00 01 10 11 128 129 143 142 141 140

    139 138 137 136 135 134 133 132 131 130 list.get (0x10001101);
  23. 00 01 10 11 00 01 10 11 128 129

    143 142 141 140 139 138 137 136 135 134 133 132 131 130 list.get (0x10001101);
  24. 00 01 10 11 00 01 10 11 00 01

    10 11 128 129 143 142 141 140 139 138 137 136 135 134 133 132 131 130 list.get (0x10001101);
  25. 00 01 10 11 00 01 10 11 00 01

    10 11 00 01 10 11 128 129 143 142 141 140 139 138 137 136 135 134 133 132 131 130 list.get (0x10001101);
  26. 00 01 10 11 00 01 10 11 00 01

    10 11 00 01 10 11 128 129 143 142 141 140 139 138 137 136 135 134 133 132 131 130 list.get (141);
  27. 128 129 143 142 141 140 139 138 137 136

    135 134 133 132 131 130 list.set (141, “foo”);
  28. 128 129 143 142 141 140 139 138 137 136

    135 134 133 132 131 130 list.set (141, “foo”);
  29. 128 129 143 142 141 140 139 138 137 136

    135 134 133 132 131 130 list.set (141, “foo”); “foo”
  30. Further Readings and References 1. Rich Hickey, Value of Values

    - https://www.infoq.com/presentations/Value-Values 2. David Nolen, Mori - https://github.com/swannodette/mori 3. Immutable.js by Facebook - https://facebook.github.io/immutable-js/ 4. Elm Language - elm-lang.org 5. Ideal Hash Trees, Phil Bagwell - http://lampwww.epfl.ch/papers/idealhashtrees.pdf 6. Lee Byron, React.js Conf - https://youtu.be/I7IdS-PbEgI
  31. Recap 1. Immutable data structures let us write simple and

    low overhead applications. 2. We can use it in JS applications using libraries. 3. They use structural sharing for efficient implementation. 4. They enable super fast performance.