Last active
October 13, 2025 17:01
-
-
Save jbenner-radham/bdfffe91073e727faa42bdff7d3c0f20 to your computer and use it in GitHub Desktop.
Two TS unique functions to compare and benchmark later... (now benchmarked!)
This file contains hidden or 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 { Suite } from 'bench-node'; | |
| function uniqueA(array) { | |
| return array.filter((item, index) => array.indexOf(item) === index); | |
| } | |
| function uniqueB(array) { | |
| return [...new Set(array)]; | |
| } | |
| const data = [ | |
| 'jkdjfkjdkfdf', | |
| 'erkejrjekrjekr', | |
| 'jkdjfkdjf', | |
| 'jkejrjkejrkejkrjekjrjekrjkerjr', | |
| 1337, | |
| 1337, | |
| 'jkejrjkejrkejkrjekjrjekrjkerjr', | |
| 'kjkjkjkererer', | |
| '0122222333333', | |
| '0122222333333', | |
| '0122222333333', | |
| '0122222333333', | |
| '0122222333333', | |
| '0122222333333', | |
| '0122222333333', | |
| '0122222333333', | |
| 'kjere,mr,emr,me,rm,erllekre', | |
| 'kwjekjejriererueerkrje', | |
| 'jdfjkjdfjkfkdjfjjfdj' | |
| ]; | |
| const suite = new Suite(); | |
| suite.add('Using uniqueA', () => { | |
| uniqueA(data); | |
| }); | |
| suite.add('Using uniqueB', () => { | |
| uniqueB(data); | |
| }); | |
| suite.run(); | |
| // $ node --allow-natives-syntax benchmark.js | |
| // Using uniqueA x 8,284,339 ops/sec (12 runs sampled) v8-never-optimize=true min..max=(118.72ns...122.45ns) | |
| // Using uniqueB x 5,515,393 ops/sec (11 runs sampled) v8-never-optimize=true min..max=(178.95ns...184.18ns) |
This file contains hidden or 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
| function uniqueA<T>(array: T[]): T[] { | |
| return array.filter((item, index) => array.indexOf(item) === index); | |
| } | |
| function uniqueB<T>(array: T[]): T[] { | |
| return [...new Set(array)]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment