dracoblue.net

What's faster: JSON.parse+stringify or extend/mixin?

Today I ran into the case, that I just wanted to clone a plain javascript object (no functions at all, just values). I was wondering whether it would be faster to use the native JSON.stringify+parse (which would include generating a very big string, just to throw it away):

copy = JSON.stringify(JSON.parse(entry));

or to use the javascript functions

process.mixin/extend and perform a deep copy:

copy = extend(true, {}, entry)

Here are the results:

 JSON-String: 19886421 characters
  8691ms 3x JSON.parse(JSON.stringify(object))
  5839ms 3x extend(true, {}, object)

JSON-String: 29959 characters
  10305ms 3000x JSON.parse(JSON.stringify(object))
  7445ms 3000x extend(true, {}, object)

JSON-String: 249 characters
  214ms 3000x JSON.parse(JSON.stringify(object))
  94ms 3000x extend(true, {}, object)

JSON-String: 39 characters
  118ms 3000x JSON.parse(JSON.stringify(object))
  32ms 3000x extend(true, {}, object)

The script is the following:

http://gist.github.com/354628 and the Test PC was a i7-920 Quadcore with HT and 8GB DDR3 RAM.

In my test setup it was 2x faster to use a function like extend.

In javascript, node.js by
@ 03 Apr 2010, Comments at Reddit & Hackernews

Give something back

Were my blog posts useful to you? If you want to give back, support one of these charities, too!

Report hate in social media Campact e.V. With our technology and your help, we protect the oceans from plastic waste. Gesellschaft fur Freiheitsrechte e. V. The civil eye in the mediterranean

Recent Dev-Articles

Read recently

Recent Files

About