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 DracoBlue @ 03 Apr 2010 | 170 Words

process.mixin deprecated in nodejs

When you are using process.mixin within nodejs 0.1.33, you'll get the following message:

deprecation warning: process.mixin will
be removed from node-core future releases

That this function is not in process anymore, is correct. It has nothing to do with processes. That said, you may think if you really need process.mixin at all. This function is available to create a (deep) copy of an entire object and it's properties.

In my case, I wanted to keep the ability to extend a prototype of an object with an aspect like "Logging".

Thatswhy I called the function

extend and borrowed the implementation from jquery (also licensed under the terms of MIT).

Where to get this "extend"-function from? Option 1: Copy the process.mixin from nodejs 0.1.30 Option 2: Take extend from jquery and modify it so it fits nodejs Option 3: Copy the nodejs version of jquerys extend from my util.js :)

Now I can easily do:

extend(true, Controller.prototype, Logging.prototype)

and every new Controller will have the Logging features, too!

By the way, there's always the possibility to do

JSON.parse(JSON.stringify(obj))

if the object does not contain any functions or those are not important to you.

In javascript, jquery, node.js by DracoBlue @ 03 Apr 2010 | 210 Words

Moving GTAT Database Server

Hello,

Now I'll move the database server to a new host. GTAT.org may not be available until 23:00.

  • Draco

In gta:tournament by DracoBlue @ 29 Mar 2010 | 21 Words

Escaping without NO_BACKSLASH_ESCAPE in Mysql

While working with Node.JS and dbslayer today, I was facing a weird issue with escaping strings.

Best thing would have been, if I could have forced my server to use NOBACKSLASHESCAPES as sql-mode and then just escape the ' to ''.

But since this server is also used by other programs and NOBACKSLASHESCAPES option cannot be set by option for dbslayer I had to turn it off again.

I ended up with this tiny escape string method:

var db_escape_string = function(string) {
    return string.replace(/([\\\n\r])/g, "\\$&").replace("'", "''", 'g');
};

It replaces \ (backslash), newline and carriage return with \, \n and \r. Since also single quote needs to be escaped, I finally replace all ' with ''.

You may wonder why I do not escape ". The issue is, that a " appearing within a string in between ' does not need to be escaped.

'this is a test"2' => 'this is a test\"2'

That's why I sticked to the rule to put strings within single quote and do not escape those question marks at all.

In javascript, mysql, node.js by DracoBlue @ 21 Mar 2010 | 188 Words

Operation failed. File system input or output error

A while back I explained how to mount sftp/ssh folders in eclipse.

When working with eclipse or zend studio on remote systems, there are times when I get the message "Operation failed. File system input or output error" very often.

Even though several posts in zend forums suggest, that you should restart your IDE in this case, I tried to track down what causes the issue.

I ended up with the following: The ssh-host has already closed the connection, but eclipse thinks it's still available. So it tries to use the connection, but fails.

Whenever I get this issue now, I go into "Remote Systems"-View, right-click the Connection and choose "Disconnect" from the context menu. Right after that, I open that context menu again and press "Connect". Problem resolved.

In eclipse by DracoBlue @ 14 Mar 2010 | 131 Words

Page 19 - Page 20 - Page 21