Skip to content

Commit

Permalink
refactor(array): update
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib committed Jan 4, 2025
1 parent 08c8c88 commit 1527502
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const draftsCache = new WeakSet<object>();

let arrayHandling = false;

const proxyArrayMethods = ['splice', 'shift', 'unshift', 'reverse'];

const proxyHandler: ProxyHandler<ProxyDraft> = {
get(target: ProxyDraft, key: string | number | symbol, receiver: any) {
const copy = target.copy?.[key];
Expand Down Expand Up @@ -93,7 +95,7 @@ const proxyHandler: ProxyHandler<ProxyDraft> = {
const desc = getDescriptor(source, key);
const value = desc?.value;
if (target.type === DraftType.Array) {
if (['splice', 'shift', 'unshift', 'reverse'].includes(key as string)) {
if (proxyArrayMethods.includes(key as string)) {
return function (this: any, ...args: any[]) {
let returnValue: any;
arrayHandling = true;
Expand Down
1 change: 1 addition & 0 deletions test/performance/benchmark-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from 'immer';
import { create } from '../..';

// repo: https://github.com/markerikson/immer-perf-tests
function createInitialState() {
const initialState = {
largeArray: Array.from({ length: 10000 }, (_, i) => ({
Expand Down
1 change: 1 addition & 0 deletions test/performance/benchmark-reducer1.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { produce as produce10, setAutoFreeze as setAutoFreeze10 } from 'immer';
import { create as produceMutative } from '../../dist/mutative.esm.mjs';
import { bench, run, summary } from 'mitata';

// repo: https://github.com/markerikson/immer-perf-tests
function createInitialState() {
const initialState = {
largeArray: Array.from({ length: 10000 }, (_, i) => ({
Expand Down

0 comments on commit 1527502

Please sign in to comment.