DEV Community

loading...

Discussion on: Deal with javascript objects faster !!!

Collapse
peerreynders profile image
peerreynders

Create an object with properties removed.

let a, b, redacted;
({ a, b, ...redacted } = { a: 10, b: 20, c: 30, d: 40 }); // Note the enclosing parentheses to reuse existing names
console.assert(a === 10);
console.assert(b === 20);
console.assert(equivalent(redacted, { c: 30, d: 20 }));

function equivalent(actual, expected) {
  const props = new Map(Object.entries(expected));
  for (const [key, value] of Object.entries(actual)) {
    const val = props.get(key);
    if (val === undefined || val !== value) return false;
    props.delete(key);
  }
  return props.size === 0;
}
Enter fullscreen mode Exit fullscreen mode
Forem Open with the Forem app