mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-23 05:06:43 +00:00
Optimize algorithm performance
This commit is contained in:
parent
d2ff2d4c8b
commit
f651283bb7
1 changed files with 81 additions and 0 deletions
81
src/algorithms/optimized.js
Normal file
81
src/algorithms/optimized.js
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
/**
|
||||
* Optimized algorithm implementation
|
||||
* Improved performance and memory usage
|
||||
*/
|
||||
|
||||
// Optimized sorting algorithm
|
||||
function optimizedSort(array, compareFn) {
|
||||
// Use native sort for better performance
|
||||
return array.slice().sort(compareFn);
|
||||
}
|
||||
|
||||
// Optimized data structure
|
||||
class OptimizedMap {
|
||||
constructor() {
|
||||
this.data = new Map();
|
||||
this.size = 0;
|
||||
}
|
||||
|
||||
set(key, value) {
|
||||
const exists = this.data.has(key);
|
||||
this.data.set(key, value);
|
||||
|
||||
if (!exists) {
|
||||
this.size++;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
get(key) {
|
||||
return this.data.get(key);
|
||||
}
|
||||
|
||||
has(key) {
|
||||
return this.data.has(key);
|
||||
}
|
||||
|
||||
delete(key) {
|
||||
const exists = this.data.has(key);
|
||||
if (exists) {
|
||||
this.data.delete(key);
|
||||
this.size--;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.data.clear();
|
||||
this.size = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Memory-efficient data processing
|
||||
function processLargeDataset(dataset) {
|
||||
const results = [];
|
||||
const batchSize = 1000;
|
||||
|
||||
for (let i = 0; i < dataset.length; i += batchSize) {
|
||||
const batch = dataset.slice(i, i + batchSize);
|
||||
const processed = batch.map(item => ({
|
||||
...item,
|
||||
processed: true
|
||||
}));
|
||||
|
||||
results.push(...processed);
|
||||
|
||||
// Allow garbage collection
|
||||
if (i % 10000 === 0) {
|
||||
await new Promise(resolve => setImmediate(resolve));
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
optimizedSort,
|
||||
OptimizedMap,
|
||||
processLargeDataset
|
||||
};
|
||||
Loading…
Reference in a new issue