-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimize(sandbox): remove object spread operator for faster performan…
…ce in big array iterator
- Loading branch information
Showing
4 changed files
with
36 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* @author Kuitos | ||
* @since 2023-11-15 | ||
*/ | ||
/** | ||
* transform the array to a truthy object for better performance with in operator check later | ||
* @param array | ||
*/ | ||
export function array2TruthyObject(array: string[]): Record<string, true> { | ||
return array.reduce( | ||
(obj, key) => { | ||
obj[key] = true; | ||
return obj; | ||
}, | ||
// Notes that babel will transpile spread operator to Object.assign({}, ...args), which will keep the prototype of Object in merged object, | ||
// while this result used as Symbol.unscopables, it will make properties in Object.prototype always be escaped from proxy sandbox as unscopables check will look up prototype chain as well, | ||
// such as hasOwnProperty, toString, valueOf, etc. | ||
// so we should use Object.create(null) to create a pure object without prototype chain here. | ||
Object.create(null) as Record<string, true>, | ||
); | ||
} |