-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE]: unit tests and benchmarks
- Loading branch information
1 parent
d1548ae
commit eb28ee0
Showing
72 changed files
with
4,941 additions
and
2,436 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 |
---|---|---|
|
@@ -130,4 +130,7 @@ dist | |
.pnp.* | ||
|
||
# IDE | ||
.idea | ||
.idea | ||
|
||
# documentation | ||
docs |
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 |
---|---|---|
@@ -1 +1 @@ | ||
#pnpm test | ||
pnpm 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import { performance } from 'perf_hooks'; | ||
|
||
import { | ||
binarySearch, | ||
exponentialSearch, | ||
heapSort, | ||
hybridSearch, | ||
linearSearch, | ||
mergeSort, | ||
ternarySearch, | ||
timSort, | ||
} from './src/index.ts'; | ||
|
||
const DATA_SIZES = { | ||
SMALL: 1000, | ||
MEDIUM: 10000, | ||
LARGE: 100000, | ||
EXTRA_LARGE: 1000000, | ||
}; | ||
|
||
type BenchmarkResult = { | ||
algorithm: string; | ||
operation: string; | ||
size: number; | ||
time: number; | ||
}; | ||
|
||
function generateData(size: number): number[] { | ||
return Array.from({ length: size }, () => Math.floor(Math.random() * size)); | ||
} | ||
|
||
function benchmarkSortAlgorithms(data: number[], size: number): BenchmarkResult[] { | ||
const results: BenchmarkResult[] = []; | ||
|
||
const startHeapSort = performance.now(); | ||
heapSort([...data], (a, b) => a - b); | ||
results.push({ | ||
algorithm: 'Heap Sort', | ||
operation: 'Sort', | ||
size, | ||
time: performance.now() - startHeapSort, | ||
}); | ||
|
||
const startMergeSort = performance.now(); | ||
mergeSort([...data], (a, b) => a - b); | ||
results.push({ | ||
algorithm: 'Merge Sort', | ||
operation: 'Sort', | ||
size, | ||
time: performance.now() - startMergeSort, | ||
}); | ||
|
||
const startTimSort = performance.now(); | ||
timSort([...data], (a, b) => a - b); | ||
results.push({ | ||
algorithm: 'Tim Sort', | ||
operation: 'Sort', | ||
size, | ||
time: performance.now() - startTimSort, | ||
}); | ||
|
||
const startNativeSort = performance.now(); | ||
[...data].sort((a, b) => a - b); | ||
results.push({ | ||
algorithm: 'Native Sort', | ||
operation: 'Sort', | ||
size, | ||
time: performance.now() - startNativeSort, | ||
}); | ||
|
||
return results; | ||
} | ||
|
||
function benchmarkSearchAlgorithms( | ||
data: number[], | ||
target: number, | ||
size: number, | ||
): BenchmarkResult[] { | ||
const results: BenchmarkResult[] = []; | ||
|
||
// Ensure data is sorted for binary search variants | ||
const sortedData = [...data].sort((a, b) => a - b); | ||
|
||
const startBinarySearch = performance.now(); | ||
binarySearch(sortedData, target, { compareFn: (a, b) => a - b, isSorted: true }); | ||
results.push({ | ||
algorithm: 'Binary Search', | ||
operation: 'Search', | ||
size, | ||
time: performance.now() - startBinarySearch, | ||
}); | ||
|
||
const startExponentialSearch = performance.now(); | ||
exponentialSearch(sortedData, target, { compareFn: (a, b) => a - b, isSorted: true }); | ||
results.push({ | ||
algorithm: 'Exponential Search', | ||
operation: 'Search', | ||
size, | ||
time: performance.now() - startExponentialSearch, | ||
}); | ||
|
||
const startHybridSearch = performance.now(); | ||
hybridSearch(sortedData, target, { compareFn: (a, b) => a - b, isSorted: true }); | ||
results.push({ | ||
algorithm: 'Hybrid Search', | ||
operation: 'Search', | ||
size, | ||
time: performance.now() - startHybridSearch, | ||
}); | ||
|
||
const startLinearSearch = performance.now(); | ||
linearSearch(data, target, (a, b) => a - b); | ||
results.push({ | ||
algorithm: 'Linear Search', | ||
operation: 'Search', | ||
size, | ||
time: performance.now() - startLinearSearch, | ||
}); | ||
|
||
const startTernarySearch = performance.now(); | ||
ternarySearch(sortedData, target, 0, sortedData.length - 1, { | ||
compareFn: (a, b) => a - b, | ||
isSorted: true, | ||
}); | ||
results.push({ | ||
algorithm: 'Ternary Search', | ||
operation: 'Search', | ||
size, | ||
time: performance.now() - startTernarySearch, | ||
}); | ||
|
||
return results; | ||
} | ||
|
||
function runBenchmarks(): BenchmarkResult[] { | ||
const allResults: BenchmarkResult[] = []; | ||
|
||
for (const [, size] of Object.entries(DATA_SIZES)) { | ||
const data = generateData(size); | ||
const target = data[Math.floor(Math.random() * size)]; | ||
|
||
// Sorting Benchmarks | ||
allResults.push(...benchmarkSortAlgorithms(data, size)); | ||
|
||
// Searching Benchmarks | ||
allResults.push(...benchmarkSearchAlgorithms(data, target, size)); | ||
} | ||
|
||
return allResults; | ||
} | ||
|
||
// Run the benchmark and log the results | ||
const benchmarkResults = runBenchmarks(); | ||
console.table(benchmarkResults); |
Oops, something went wrong.