From b03752cbe0b5f20708cf49e4e966bfa6d0b1587d Mon Sep 17 00:00:00 2001 From: Lauren Tan Date: Mon, 17 Oct 2022 16:07:50 -0400 Subject: [PATCH] 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 --- .../forget/packages/react-forget-runtime/index.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/compiler/forget/packages/react-forget-runtime/index.js b/compiler/forget/packages/react-forget-runtime/index.js index 3f0feafdc8..7ae3cb82cb 100644 --- a/compiler/forget/packages/react-forget-runtime/index.js +++ b/compiler/forget/packages/react-forget-runtime/index.js @@ -16,9 +16,12 @@ const { export const $empty = Symbol.for("react.usememocache_sentinel"); -export function unstable_useMemoCache(i) { +export function unstable_useMemoCache(size) { "use no forget"; - const $ = new Array(i).fill($empty); + const $ = new Array(size); + for (let ii = 0; ii < size; ii++) { + $[ii] = $empty; + } return useRef($).current; } @@ -78,7 +81,9 @@ export function $endLazy() { } export function $reset($) { - $.fill($empty); + for (let ii = 0; ii < $.length; ii++) { + $[ii] = $empty; + } } export function $makeReadOnly() {