Skip to content

Commit

Permalink
perf: minor performance improvements, regex in replaceAll
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyshlyaev177 committed Nov 13, 2024
1 parent bab7990 commit ea6bc70
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const Comp1 = ({ searchParams }: { searchParams?: object }) => {
onChange={(ev) =>
setUrl((curr) => ({ ...curr, perPage: +ev.target.value }))
}
className=""
data-testid="select"
>
<option>10</option>
Expand Down
2 changes: 1 addition & 1 deletion packages/example-nextjs14/src/app/Status-for-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const Status = ({ className, sp }: { className?: string; sp?: object }) => {
<div className="font-semibold mb-2 ">
Other client component
</div>
<h3 className="">Types and structure of data are presered</h3>
<h3>Types and structure of data are presered</h3>

<div className="flex-none">
<pre
Expand Down
2 changes: 1 addition & 1 deletion packages/example-nextjs14/src/app/Status.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const Status = ({
<div className="font-semibold mb-2 ">
Other client component
</div>
<h3 className="">Types and structure of data are presered</h3>
<h3>Types and structure of data are presered</h3>

<div className="flex-none">
<pre
Expand Down
2 changes: 1 addition & 1 deletion packages/example-nextjs15/src/app/Status-for-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const Status = ({ className, sp }: { className?: string; sp?: object }) => {
<div className="font-semibold mb-2">
Other client component
</div>
<h3 className="">Types and structure of data are presered</h3>
<h3>Types and structure of data are presered</h3>

<div className="flex-none">
<pre
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const Component = () => {
onChange={(ev) =>
setUrl((curr) => ({ ...curr, perPage: +ev.target.value }))
}
className=""
data-testid="select"
>
<option>10</option>
Expand Down
2 changes: 1 addition & 1 deletion packages/example-react-router6/src/Status-for-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Status = ({ className }: { className?: string }) => {
return (
<div className={className}>
<div className="font-semibold mb-2">Other client component</div>
<h3 className="">Types and structure of data are presered</h3>
<h3>Types and structure of data are presered</h3>

<div className="flex-none">
<pre
Expand Down
6 changes: 3 additions & 3 deletions packages/urlstate/encoder/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export function encode(payload: unknown): string {
return SYMBOLS.undefined;
default:
return JSON.stringify(payload)
.replaceAll("'", "%27")
.replaceAll('"', "'");
.replaceAll(/'/g, "%27")
.replaceAll(/"/g, "'");
}
}

Expand All @@ -49,7 +49,7 @@ export type Primitive = Exclude<
*/
export function decode<T>(payload: string, fallback?: T) {
return parseJSON(
payload.replaceAll("'", '"').replaceAll("%27", "'"),
payload.replaceAll(/'/g, '"').replaceAll(/%27/g, "'"),
fallback as JSONCompatible,
);
}
Expand Down
10 changes: 5 additions & 5 deletions packages/urlstate/useUrlStateBase/useUrlStateBase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ describe('useUrlStateBase', () => {

expect(result.current.state).toStrictEqual({ ...shape, num: 50 });
expect(router.push).toHaveBeenCalledTimes(1);
expect(router.push).toHaveBeenNthCalledWith(1, '/?num=50', {});
expect(router.push).toHaveBeenNthCalledWith(1, '/?num=50');
});

it('partial shape', async () => {
Expand All @@ -244,7 +244,7 @@ describe('useUrlStateBase', () => {

expect(result.current.state).toStrictEqual({ ...shape, num: 50 });
expect(router.push).toHaveBeenCalledTimes(1);
expect(router.push).toHaveBeenNthCalledWith(1, '/?num=50', {});
expect(router.push).toHaveBeenNthCalledWith(1, '/?num=50');
});

it('no arg, should reset', async () => {
Expand All @@ -262,7 +262,7 @@ describe('useUrlStateBase', () => {

expect(result.current.state).toStrictEqual({ ...shape, num: 50 });
expect(router.push).toHaveBeenCalledTimes(1);
expect(router.push).toHaveBeenNthCalledWith(1, '/?num=50', {});
expect(router.push).toHaveBeenNthCalledWith(1, '/?num=50');
});
});

Expand All @@ -278,7 +278,7 @@ describe('useUrlStateBase', () => {

expect(result.current.state).toStrictEqual({ ...shape, num: 50 });
expect(router.push).toHaveBeenCalledTimes(1);
expect(router.push).toHaveBeenNthCalledWith(1, '/?num=50', {});
expect(router.push).toHaveBeenNthCalledWith(1, '/?num=50');
});

it('do not update if url same', () => {
Expand Down Expand Up @@ -312,7 +312,7 @@ describe('useUrlStateBase', () => {
await new Promise(process.nextTick);

expect(router.push).toHaveBeenCalledTimes(1);
expect(router.push).toHaveBeenNthCalledWith(1, `/?num=55${hash}`, {});
expect(router.push).toHaveBeenNthCalledWith(1, `/?num=55${hash}`);
});

it('replace and options', async () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/urlstate/useUrlStateBase/useUrlStateBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ export function useUrlStateBase<T extends JSONCompatible>(
let upd: (typeof queue.current)[0] | undefined;
setState(newVal);

const { replace, ...rOptions } = options || {};

queue.current.push([replace ? "replace" : "push", newUrl, rOptions]);
const replace = options?.replace;
delete options?.replace;
queue.current.push([replace ? "replace" : "push", newUrl, options]);

if (queue.current.length === 1)
queueMicrotask(() => {
Expand Down Expand Up @@ -129,7 +129,7 @@ function getOtherParams<T extends object>(shape: T) {
type UpdateQueueItem = [
method: "push" | "replace",
url: string,
opts: Partial<Options>,
opts?: Partial<Options>,
];

const popstateEv = "popstate";
Expand Down
2 changes: 1 addition & 1 deletion packages/urlstate/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function filterUnknown<T extends object>(

return entries
.filter(([key]) => shapeKeys.includes(key))
.map(([key, val]) => [key.replaceAll("+", " "), val]);
.map(([key, val]) => [key.replaceAll(/\+/g, " "), val]);
}

export function assignValue<T extends object>(shape: T, newVal: Partial<T>) {
Expand Down

0 comments on commit ea6bc70

Please sign in to comment.