@@ -9,14 +9,11 @@ function batch<T>(fn: () => T): T
99```
1010
1111` batch ` is a low - level API that batches updates together .
12- More precisely , ` batch(fn) ` holds the execution of downstream computations
13- during the ` fn ` block , executing them all together once the block ` fn ` returns .
14- Thus , instead of a downstream computation executing after every dependency
15- update , it will update just once at the end of the batch .
12+ More precisely , ` batch(fn) ` holds the execution of downstream computations during the ` fn ` block , executing them all together once the block ` fn ` returns .
13+ Thus , instead of a downstream computation executing after every dependency update , it will update just once at the end of the batch .
1614
1715Batching improves performance by avoiding unnecessary recalculation .
18- Suppose you have a downstream memo ` down ` that depends on
19- multiple upstream signals ` up1 ` , ` up2 ` , and ` up3 ` :
16+ Suppose you have a downstream memo ` down ` that depends on multiple upstream signals ` up1 ` , ` up2 ` , and ` up3 ` :
2017
2118` ` ` ts
2219import { createSignal, createMemo, createEffect } from "solid-js"
@@ -28,17 +25,15 @@ const down = createMemo(() => up1() + up2() + up3())
2825createEffect(() => console.log(down())) // outputs 6
2926` ` `
3027
31- If you directly update all of the upstream signals outside of batch mode ,
32- then ` down ` will recompute every time .
28+ If you directly update all of the upstream signals outside of batch mode , then ` down ` will recompute every time .
3329
3430` ` ` ts
3531setUp1(4) // recomputes down, outputs 9
3632setUp2(5) // recomputes down, outputs 12
3733setUp3(6) // recomputes down, outputs 15
3834` ` `
3935
40- If instead you update the upstream signals within a ` batch ` , then ` down `
41- will update only once at the end :
36+ If instead you update the upstream signals within a ` batch ` , then ` down ` will update only once at the end :
4237
4338` ` ` ts
4439batch(() => {
@@ -48,33 +43,21 @@ batch(() => {
4843}) // recomputes down, outputs 30
4944` ` `
5045
51- The impact is even more dramatic if you have * m * downstream computations
52- (memos , effects , etc .) that each depends on * n * upstream signals .
53- Without batching , modifying all * n * upstream signals
54- would cause * m n * updates to the downstream computations .
55- With batching , modifying all * n * upstream signals
56- would cause * m * updates to the downstream computations .
57- Given that each update takes at least * n * time
58- (just to read the upstream signals ), this cost savings can be significant .
59- Batching is also especially helpful when the downstream effects include
60- DOM updates , which can be expensive .
46+ The impact is even more dramatic if you have * m * downstream computations (memos , effects , etc .) that each depends on * n * upstream signals .
47+ Without batching , modifying all * n * upstream signals would cause * m n * updates to the downstream computations .
48+ With batching , modifying all * n * upstream signals would cause * m * updates to the downstream computations .
49+ Given that each update takes at least * n * time (just to read the upstream signals ), this cost savings can be significant .
50+ Batching is also especially helpful when the downstream effects include DOM updates , which can be expensive .
6151
62- Solid uses ` batch ` internally to automatically batch updates for you
63- in a few cases :
52+ Solid uses ` batch ` internally to automatically batch updates for you in a few cases :
6453
65- * Within [` createEffect ` ](/ reference / basic - reactivity / create - effect )
66- and [` onMount ` ](/ reference / lifecycle / on - mount )
67- (unless they are outside a [root ](/ reference / reactive - utilities / create - root ))
68- * Within the [setter of a store ](/ reference / store - utilities / create - store #setter )
69- (which can update several properties at once )
70- * Within array methods (e .g . ` Array.prototype.splice ` ) of a
71- [mutable store ](/ reference / store - utilities / create - mutable )
72- (which can update several elements at once )
54+ * Within [` createEffect ` ](/ reference / basic - reactivity / create - effect ) and [` onMount ` ](/ reference / lifecycle / on - mount ) (unless they are outside a [root ](/ reference / reactive - utilities / create - root ))
55+ * Within the [setter of a store ](/ reference / store - utilities / create - store #setter ) (which can update several properties at once )
56+ * Within array methods (e .g . ` Array.prototype.splice ` ) of a [mutable store ](/ reference / store - utilities / create - mutable ) (which can update several elements at once )
7357
7458These save you from having to use ` batch ` yourself in many cases .
75- For the most part , automatic batching should be transparent to you ,
76- because accessing a signal or memo will cause it to update if it is out of date
77- (as of Solid 1.4 ). For example :
59+ For the most part , automatic batching should be transparent to you , because accessing a signal or memo will cause it to update if it is out of date (as of Solid 1.4 ).
60+ For example :
7861
7962` ` ` ts
8063batch(() => {
@@ -88,9 +71,6 @@ batch(() => {
8871}) // recomputes down, outputs 36
8972` ` `
9073
91- You can think of ` batch(fn) ` as setting a global " batch mode" variable ,
92- calling the function `fn`, and then restoring the global variable to its
93- previous value.
74+ You can think of ` batch(fn) ` as setting a global " batch mode" variable , calling the function `fn`, and then restoring the global variable to its previous value.
9475This means that you can nest `batch` calls, and they will form one big batch.
95- It also means that, if `fn` is asynchronous,
96- only the updates before the first `await` will be batched.
76+ It also means that, if `fn` is asynchronous, only the updates before the first `await` will be batched.
0 commit comments