dracoblue.net

bash tip: for each line in a file

When you want execute an action on a set of files (all, which contain testfile), this technique is pretty straight forward:

for line in `find . | grep "testfile"`
do
    rm "$line"
done

But this actually does not work, if the file name contains spaces!

In this cases the "while read"-construct is failsafe:

find . | grep "testfile" | while read line
do
    rm "$line"
done

In bash by DracoBlue @ 05 Apr 2010 | 76 Words

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

Page 18 - Page 19 - Page 20

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