Merging static objects

JavaScript performance comparison

Test case created by flo-sch

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
      
<script>
Benchmark.prototype.setup = function() {
  const source1 = { a: '1' };
  const source2 = { b: '2' };
  const source3 = { c: '3' };

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Spread Operator
const destination = {
  ...source1,
  ...source2,
  ...source3
};
pending…
Object.assign
const destination = Object.assign(
  {},
  source1,
  source2,
  source3
);
pending…
Object.fromEntries
const destination = Object.fromEntries(
  Object.entries(source1)
    .concat(
      Object.entries(source2),
      Object.entries(source3)
    )
);
pending…
Loop through properties
let property;
const destination = {};

for (property in source1) {
  if (source1.hasOwnProperty(property)) {
    destination[property] = source1[property];
  }
}
for (property in source2) {
  if (source2.hasOwnProperty(property)) {
    destination[property] = source2[property];
  }
}
for (property in source3) {
  if (source3.hasOwnProperty(property)) {
    destination[property] = source3[property];
  }
}
pending…
Lodash _.merge
const destination = _.merge(source1, source2, source3);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments