Why are immutable objects more efficient?
Object variables are referencing heap addresses
todos
stack
@ 0x005310
name of
variable
heap
...
...
0x005310
todos = [ {
description: "task",
isImportant: false,
isDone: true
}, {...}, {...}, {...}, ...]
Mutable object change
todos
@ 0x005310
name of
variable
...
...
0x005310
todos = [ {
description: "task",
isImportant: false,
isDone: true
}, {...}, {...}, {...}, ...]
todos[0].isDone = false
false
stack
heap
Check mutable object change
...
todos = [ {
description: "task",
isImportant: false,
isDone: true
}, {...}, {...}, {...}, ...]
false
Means check each property of each element of the object
todos = [ {
description: "task",
isImportant: false,
isDone: true
},
{
description: "task",
isImportant: false,
isDone: true
}
, ...]
heap
Immutable object change
todos
@ 0x005310
name of
variable
...
...
0x005310
invalid memory
todos[0].isDone = false
false
todos = [ {
description: "task",
isImportant: false,
isDone:
}, {...}, {...}, {...}, ...]
0x008F00
@ 0x008F00
todos = [ {
description: "task",
isImportant: false,
isDone: true
}, {...}, {...}, {...}, ...]
stack
heap
Check Immutable object change
todos
stack
@ 0x005310
name of
variable
...
Means check only the address of the object pointer
What does immutability mean?
By Enrique Oriol
What does immutability mean?
- 56