-
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.
perf: optimize memory and CPU usage in prototype property probing
- Replaced object literals with Maps for storing prototype properties, enhancing memory efficiency.
- Loading branch information
1 parent
869ff5d
commit 4a67a8a
Showing
1 changed file
with
19 additions
and
16 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 |
---|---|---|
@@ -1,25 +1,28 @@ | ||
const types = [Object, String, Number, Array, Function, Boolean]; | ||
const types = [Object, String, Number, Array, Function]; | ||
|
||
const prototypesPropertiesReducer = (acc, type) => ({ | ||
...acc, | ||
[type.name]: Object.getOwnPropertyNames(type.prototype), | ||
}); | ||
const prototypePropertyNames = new Map(); | ||
types.forEach(type => prototypePropertyNames.set(type.name, Object.getOwnPropertyNames(type.prototype))); | ||
|
||
const prototypePropertyNames = () => types.reduce(prototypesPropertiesReducer, {}); | ||
const probe = () => { | ||
const userDefinedProps = []; | ||
|
||
prototypePropertyNames.forEach((props, key) => { | ||
const prototype = window[key]?.prototype; | ||
|
||
for (let i = 0; i < props.length; i++) { | ||
const prop = props[i]; | ||
let propValue; | ||
|
||
const probe = () => | ||
Object.keys(prototypePropertyNames()).reduce((acc, key) => { | ||
for (let propKey of prototypePropertyNames()[key]) { | ||
const payload = `${key}.prototype.${propKey}`; | ||
try { | ||
const propValue = eval(payload); | ||
// Check if property is user defined and not native | ||
propValue = prototype[prop]; | ||
if (typeof propValue === 'function' && !propValue.toString().includes('[native code]')) { | ||
acc.push(payload); | ||
userDefinedProps.push(`${key}.prototype.${prop}`); | ||
} | ||
} catch (e) { | ||
// Nothing to catch now. | ||
// Ignore errors | ||
} | ||
} | ||
return acc; | ||
}, []); | ||
}); | ||
|
||
return userDefinedProps; | ||
}; |