@samitouri / QOS-React-2 / commits / b03752cbe0

Use for loop instead of Array.prototype.fill

Some prior [microbenchmarking](https://jsbench.me/7ol98ws520/1) showed that a for loop outperformed `fill` (which is about ~60% slower). This is the same approach we use in the latest useMemoCache PR

Lauren Tan committed Oct 17, 2022 at 16:07 UTC b03752cbe0b5f20708cf49e4e966bfa6d0b1587d
1 file changed +8 -3
compiler/forget/packages/react-forget-runtime/index.js
+8 -3
@@ -16,9 +16,12 @@ const {
16
17 export const $empty = Symbol.for("react.usememocache_sentinel");
18
19 -export function unstable_useMemoCache(i) {
19 +export function unstable_useMemoCache(size) {
20 "use no forget";
21 - const $ = new Array(i).fill($empty);
21 + const $ = new Array(size);
22 + for (let ii = 0; ii < size; ii++) {
23 + $[ii] = $empty;
24 + }
25 return useRef($).current;
26 }
27
@@ -78,7 +81,9 @@ export function $endLazy() {
81 }
82
83 export function $reset($) {
81 - $.fill($empty);
84 + for (let ii = 0; ii < $.length; ii++) {
85 + $[ii] = $empty;
86 + }
87 }
88
89 export function $makeReadOnly() {