Skip to content

Instantly share code, notes, and snippets.

@petervmeijgaard
Last active May 11, 2023 14:56
Show Gist options
  • Save petervmeijgaard/3e8eb9ea6cf965ccf532cf7693bda1f2 to your computer and use it in GitHub Desktop.
Save petervmeijgaard/3e8eb9ea6cf965ccf532cf7693bda1f2 to your computer and use it in GitHub Desktop.
Object entries performance test
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