-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: speed up member list function (#867)
Previously, `MemberApi.prototype.getMany` would iterate through `allDeviceInfo` for every role. Now, it only iterates through `allDeviceInfo` once.
- Loading branch information
Showing
3 changed files
with
65 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Like [`Map.groupBy`][0], but the result's values aren't arrays. | ||
* | ||
* If multiple values resolve to the same key, an error is thrown. | ||
* | ||
* [0]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/groupBy | ||
* | ||
* @template T | ||
* @template K | ||
* @param {Iterable<T>} items | ||
* @param {(item: T) => K} callbackFn | ||
* @returns {Map<K, T>} | ||
*/ | ||
export function keyBy(items, callbackFn) { | ||
/** @type {Map<K, T>} */ const result = new Map() | ||
for (const item of items) { | ||
const key = callbackFn(item) | ||
if (result.has(key)) { | ||
throw new Error(`keyBy found duplicate key ${JSON.stringify(key)}`) | ||
} | ||
result.set(key, item) | ||
} | ||
return result | ||
} |
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,37 @@ | ||
import assert from 'node:assert/strict' | ||
import test from 'node:test' | ||
import { keyBy } from '../../src/lib/key-by.js' | ||
|
||
test('returns an empty map if passed an empty iterable', () => { | ||
assert.deepEqual( | ||
keyBy([], () => { | ||
throw new Error('Should not be called') | ||
}), | ||
new Map() | ||
) | ||
}) | ||
|
||
test('keys a list of items by a key function', () => { | ||
const items = [ | ||
{ id: 1, name: 'foo' }, | ||
{ id: 2, name: 'bar' }, | ||
{ id: 3, name: 'baz' }, | ||
] | ||
const result = keyBy(items, (item) => item.id) | ||
assert.deepEqual( | ||
result, | ||
new Map([ | ||
[1, items[0]], | ||
[2, items[1]], | ||
[3, items[2]], | ||
]) | ||
) | ||
}) | ||
|
||
test('duplicate keys', () => { | ||
const items = [ | ||
{ id: 1, name: 'foo' }, | ||
{ id: 1, name: 'bar' }, | ||
] | ||
assert.throws(() => keyBy(items, (item) => item.id)) | ||
}) |