Shallow Object Copy Maciej Cieslar

JavaScript performance comparison

Test case created by maciejcieslar

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
    const obj = {
      _id: '5bede99c0e1d33c83e6268c8',
      index: 0,
      guid: '0fb8cedb-2d45-4d6f-8ddb-6c8680588235',
      isActive: true,
      balance: '$3,822.80',
      picture: 'http://placehold.it/32x32',
      age: 26,
      eyeColor: 'blue',
      name: {
        first: 'Sally',
        last: 'Drake',
      },
      company: 'SECURIA',
      email: 'sally.drake@securia.net',
      phone: '+1 (870) 428-3009',
      address: '219 Rutledge Street, Barronett, Utah, 2541',
      about:
        'Nisi labore nostrud esse aliqua voluptate ipsum magna quis eu incididunt nulla cupidatat. Magna nisi et sint excepteur magna ea velit adipisicing culpa. Exercitation cupidatat do sunt sit eiusmod veniam exercitation. Ea sit voluptate adipisicing excepteur amet cillum aliquip amet. Et esse voluptate labore laborum ex velit eiusmod culpa enim amet ut eu excepteur commodo. Aliquip enim deserunt labore ad non mollit ullamco nulla id laboris qui est.',
      registered: 'Monday, August 10, 2015 12:25 PM',
      latitude: '-81.164075',
      longitude: '79.493894',
      tags: ['aliqua', 'ad', 'enim', 'excepteur', 'ex'],
      range: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
      friends: [
        {
          id: 0,
          name: 'Levy Brooks',
        },
        {
          id: 1,
          name: 'Joann Snider',
        },
        {
          id: 2,
          name: 'Peters Miles',
        },
      ],
      greeting: 'Hello, Sally! You have 6 unread messages.',
      favoriteFruit: 'apple',
    }
  
  function shallow1(source) {
    return {
      ...(source),
    }
  }
  
  function shallow2(source) {
    const copy = {}
  
    Object.keys(source).forEach((key) => {
      copy[key] = source[key]
    })
  
    return copy
  }
  

};
</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
v1
shallow1(obj)
pending…
v2
shallow2(obj)
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