-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(perf): up performance from quadratic to linear time (#77)
- Loading branch information
1 parent
e06e0b4
commit 6277380
Showing
29 changed files
with
211 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
|
||
#!/usr/bin/env sh | ||
source ./scripts/shared.sh | ||
|
||
npm run clean | ||
echo "┏━━━ 📦 $PACKAGE_NAME: build ━━━━━━━━━━━━━━━━━━━" | ||
echo "┏━━━ 📦 build ━━━━━━━━━━━━━━━━━━━" | ||
tsc && rollup -c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
#!/usr/bin/env sh | ||
source ./scripts/shared.sh | ||
|
||
echo "┏━━━ 🧹 $PACKAGE_NAME: clean ━━━━━━━━━━━━━━━━━━━" | ||
echo "┏━━━ 🧹 clean ━━━━━━━━━━━━━━━━━━━" | ||
rimraf ./dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
#!/usr/bin/env sh | ||
source ./scripts/shared.sh | ||
|
||
echo "┏━━━ 🕵️♀️ $PACKAGE_NAME lint ━━━━━━━" | ||
echo "┏━━━ 🕵️♀️ lint ━━━━━━━" | ||
eslint src --quiet --ext .ts |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
#!/usr/bin/env sh | ||
source ./scripts/shared.sh | ||
|
||
echo "┏━━━ 🛍️ $PACKAGE_NAME size:analyze ━━━━━━━" | ||
echo "┏━━━ 🛍️ size:analyze ━━━━━━━" | ||
size-limit --why |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
#!/usr/bin/env sh | ||
source ./scripts/shared.sh | ||
|
||
echo "┏━━━ 🛍️ $PACKAGE_NAME: size ━━━━━━━" | ||
echo "┏━━━ 🛍️ size ━━━━━━━" | ||
size-limit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env sh | ||
|
||
node --expose-gc ./node_modules/.bin/jest --runInBand --coverage --verbose --forceExit --detectOpenHandles --ci |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#!/usr/bin/env sh | ||
|
||
npx jest | ||
npx jest --runInBand --coverage --verbose --forceExit --detectOpenHandles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env sh | ||
|
||
npx jest --runInBand --watchAll --forceExit --detectOpenHandles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const TEN_ITEMS = [ | ||
{ name: 'Dan', age: 5 }, | ||
{ name: 'Puppy', age: 5 }, | ||
{ name: 'Woofer', age: 22 }, | ||
{ name: 'Dan', age: 20 }, | ||
{ name: 'Cat', age: 11 }, | ||
{ name: 'Dog', age: 2 }, | ||
{ name: 'Pig', age: 20 }, | ||
{ name: 'Cow', age: 6 }, | ||
{ name: 'Horse', age: 9 }, | ||
{ name: 'Pig', age: 2 }, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './TEN_ITEMS'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
import { TEN_ITEMS } from '../__fixtures__'; | ||
|
||
/* eslint-disable @typescript-eslint/no-unsafe-return */ | ||
export const getNItems = (count: number) => | ||
Array.from({ length: count / TEN_ITEMS.length }).flatMap(() => TEN_ITEMS); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './getNItems'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './getNItems'; | ||
export * from './logPerformance'; | ||
export * from './__fixtures__'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './logPerformance'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const logPerformance = (opts: { | ||
name: string; | ||
fn: () => void; | ||
assertion: (actual: number) => void; | ||
}) => { | ||
const { name, fn, assertion } = opts; | ||
|
||
const start = performance.now(); | ||
fn(); | ||
const end = performance.now(); | ||
|
||
const duration = end - start; | ||
assertion(duration); | ||
|
||
console.table({ [name]: `${duration}ms` }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
export type ObjectKey = string | number | symbol; | ||
|
||
export type EntityDict<Entity, Key extends ObjectKey> = { | ||
entities: Record<Key, Entity | undefined>; | ||
entities: Record<Key, Entity[] | undefined>; | ||
ids: Key[]; | ||
}; |
5 changes: 4 additions & 1 deletion
5
src/utils/_internal/groupBy.ts → src/utils/__internal__/groupBy.slow.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { EntityDict } from '../../typings'; | ||
|
||
/** | ||
* O(n) implementation of groupBy | ||
*/ | ||
export const groupBy = <K extends keyof T, T extends Record<K, T[K]>>( | ||
key: K, | ||
items: T[] | ||
) => { | ||
const result = { entities: {}, ids: [] } as EntityDict<T, T[K]>; | ||
|
||
for (let i = 0; i < items.length; i++) { | ||
if (result.entities[items[i][key]]) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
result.entities[items[i][key]]!.push(items[i]); | ||
} else { | ||
result.entities[items[i][key]] = [items[i]]; | ||
result.ids.push(items[i][key]); | ||
} | ||
} | ||
|
||
return result; | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { getNItems, logPerformance } from '../../test-helpers'; | ||
import { createGroup } from './createGroup'; | ||
|
||
const ONE_THOUSAND_ITEMS = getNItems(1000); | ||
const TEN_THOUSAND_ITEMS = getNItems(10_000); | ||
const ONE_MILLION_ITEMS = getNItems(1_000_000); | ||
const TEN_MILLION_ITEMS = getNItems(10_000_000); | ||
|
||
const TEN_MILLISECONDS = 10; | ||
const ONE_HUNDRED_MILLISECONDS = 100; | ||
const FIVE_HUNDRED_MILLISECONDS = 500; | ||
const ONE_SECOND = 1000; | ||
|
||
const byAge = createGroup('age'); | ||
|
||
describe('createGroup - performance', () => { | ||
it('[One thousand items]: executes in less than 10ms', (done) => { | ||
expect(ONE_THOUSAND_ITEMS.length).toBe(1000); | ||
|
||
logPerformance({ | ||
name: 'One thousand items', | ||
fn: () => byAge(ONE_THOUSAND_ITEMS), | ||
assertion: (milliseconds) => { | ||
expect(milliseconds).toBeLessThan(TEN_MILLISECONDS); | ||
global.gc?.(); | ||
done(); | ||
}, | ||
}); | ||
}); | ||
|
||
test('[Ten thousand items]: executes in less than 100ms', (done) => { | ||
expect(TEN_THOUSAND_ITEMS.length).toBe(10_000); | ||
|
||
logPerformance({ | ||
name: 'Ten thousand items', | ||
fn: () => byAge(TEN_THOUSAND_ITEMS), | ||
assertion: (milliseconds) => { | ||
expect(milliseconds).toBeLessThan(ONE_HUNDRED_MILLISECONDS); | ||
global.gc?.(); | ||
done(); | ||
}, | ||
}); | ||
}); | ||
it('[One million items]: executes in less than 500ms', (done) => { | ||
expect(ONE_MILLION_ITEMS.length).toBe(1_000_000); | ||
|
||
logPerformance({ | ||
name: 'One Million Items', | ||
fn: () => byAge(ONE_MILLION_ITEMS), | ||
assertion: (actual) => { | ||
expect(actual).toBeLessThan(FIVE_HUNDRED_MILLISECONDS); | ||
global.gc?.(); | ||
done(); | ||
}, | ||
}); | ||
}); | ||
|
||
test('[Ten million items]: executes in less than 1s', (done) => { | ||
expect(TEN_MILLION_ITEMS.length).toBe(10_000_000); | ||
|
||
logPerformance({ | ||
name: 'Ten Million Items', | ||
fn: () => byAge(TEN_MILLION_ITEMS), | ||
assertion: (actual) => { | ||
expect(actual).toBeLessThan(ONE_SECOND); | ||
global.gc?.(); | ||
done(); | ||
}, | ||
}); | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters