-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmarks.js
More file actions
43 lines (35 loc) · 1.69 KB
/
benchmarks.js
File metadata and controls
43 lines (35 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// ==========================================
// Benchmark: Array.find() vs Map.get()
// ==========================================
// This script demonstrates the performance difference between O(N) Array lookup
// and O(1) Map lookup (Hash tables).
//
// Run this file independently using: node benchmarks.js
const ITEMS_COUNT = 100000;
const SEARCH_TARGET = 99999; // Worst case scenario for Array search (finding the very last item)
console.log(`Setting up data structures with ${ITEMS_COUNT} items...\n`);
// 1. Setup Array Data
const arrayData = [];
for (let i = 0; i < ITEMS_COUNT; i++) {
arrayData.push({ id: `task-${i}`, val: `Demo value-${i}` });
}
// 2. Setup Map Data
const mapData = new Map();
for (let i = 0; i < ITEMS_COUNT; i++) {
mapData.set(`task-${i}`, { id: `task-${i}`, val: `Demo value-${i}` });
}
console.log(`Searching for item ID 'task-${SEARCH_TARGET}'...`);
console.log('----------------------------------------------------');
// Benchmark Array (O(N) Time Complexity)
// The engine must iterate through elements sequentially until it finds a match.
console.time('Array O(N)');
const resultFromArray = arrayData.find(item => item.id === `task-${SEARCH_TARGET}`);
console.timeEnd('Array O(N)');
// Benchmark Map (O(1) Constant Time Complexity)
// The engine hashes the key and immediately knows the exact memory location.
console.time('Map O(1)');
const resultFromMap = mapData.get(`task-${SEARCH_TARGET}`);
console.timeEnd('Map O(1)');
console.log('----------------------------------------------------');
console.log('Notice how much faster the Map lookups are! As your data grows to millions');
console.log('of records in an enterprise app, O(1) Data Structures are non-negotiable.');