Skip to content

Commit

Permalink
perf: optimize memory and CPU usage in prototype property probing
Browse files Browse the repository at this point in the history
- Replaced object literals with Maps for storing prototype properties, enhancing memory efficiency.
  • Loading branch information
acuciureanu committed Dec 18, 2023
1 parent 869ff5d commit 4a67a8a
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions sandbox/js/check.payload.js
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;
};

0 comments on commit 4a67a8a

Please sign in to comment.