From f7327bddb5bbf7f9ff5fcbdea72637b49cd7269e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Tue, 13 May 2025 14:29:01 -0700 Subject: [PATCH] Add benchmark for structuredClone (#51249) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/51249 Changelog: [internal] This adds a basic benchmark for `structuredClone`, so we can optimize it in the following diff. Baseline: | (index) | Task name | Latency average (ns) | Latency median (ns) | Throughput average (ops/s) | Throughput median (ops/s) | Samples | | ------- | ------------------------ | -------------------- | ------------------- | -------------------------- | ------------------------- | ------- | | 0 | 'clone a string' | '955.81 ± 1.45%' | '902.00' | '1095925 ± 0.01%' | '1108647' | 1046239 | | 1 | 'clone a basic array' | '7684.79 ± 0.76%' | '7542.00' | '131980 ± 0.02%' | '132591' | 130128 | | 2 | 'clone a basic object' | '6286.35 ± 0.53%' | '6179.00' | '161179 ± 0.02%' | '161838' | 159075 | | 3 | 'clone a complex object' | '22446.96 ± 0.32%' | '22223.00' | '44819 ± 0.03%' | '44998' | 44550 | Reviewed By: hoxyq Differential Revision: D71407319 fbshipit-source-id: ee1c89d6ba3fd2484b2f4115cc58dc4b08e04fa7 --- .../structuredClone-benchmark-itest.js | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 packages/react-native/src/private/webapis/structuredClone/__tests__/structuredClone-benchmark-itest.js diff --git a/packages/react-native/src/private/webapis/structuredClone/__tests__/structuredClone-benchmark-itest.js b/packages/react-native/src/private/webapis/structuredClone/__tests__/structuredClone-benchmark-itest.js new file mode 100644 index 00000000000..165e087af41 --- /dev/null +++ b/packages/react-native/src/private/webapis/structuredClone/__tests__/structuredClone-benchmark-itest.js @@ -0,0 +1,44 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict-local + * @format + * @oncall react_native + */ + +import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; + +import * as Fantom from '@react-native/fantom'; +import structuredClone from 'react-native/src/private/webapis/structuredClone/structuredClone'; + +const basicArray = [1, 2, 3]; +const basicObject = {a: 1, b: 2, c: 3}; +const complexObject = { + a: 1, + b: 'foo', + c: false, + d: undefined, + e: { + f: { + g: [1, 'foo', 'bar'], + }, + }, +}; + +Fantom.unstable_benchmark + .suite('structuredClone') + .test('clone a string', () => { + structuredClone('hello world'); + }) + .test('clone a basic array', () => { + structuredClone(basicArray); + }) + .test('clone a basic object', () => { + structuredClone(basicObject); + }) + .test('clone a complex object', () => { + structuredClone(complexObject); + });