Last active
May 11, 2023 14:56
-
-
Save petervmeijgaard/3e8eb9ea6cf965ccf532cf7693bda1f2 to your computer and use it in GitHub Desktop.
Object entries performance test
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
import * as R from "remeda"; | |
const generateInput = (amountOfEntries: number) => { | |
const entries = Array.from<never[], [string, string]>( | |
{ length: amountOfEntries }, | |
(_, index) => [`KEY_${index}`, `VALUE_${index}`] | |
); | |
return Object.fromEntries(entries); | |
}; | |
const withDefault = (input: Record<string, string>) => | |
Object.fromEntries(Object.entries(input)); | |
const withRemeda = (input: Record<string, string>) => | |
R.pipe(input, R.toPairs, R.fromPairs); | |
const withReduce = (input: Record<string, string>) => | |
Object.entries(input).reduce( | |
(acc, [key, value]) => ({ ...acc, [key]: value }), | |
{} | |
); | |
(() => { | |
const input = generateInput(10_000); | |
console.time("withDefault"); | |
withDefault(input); | |
console.timeEnd("withDefault"); | |
console.time("withRemeda"); | |
withRemeda(input); | |
console.timeEnd("withRemeda"); | |
console.time("withReduce"); | |
withReduce(input); | |
console.timeEnd("withReduce"); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment