The Dict Pattern

February 1, 2014

So after a long day in work looking through the JavaScript subreddit I came across a post about NEDB (Node Embedded database) its a persistent database for Node.js written in JavaScript with no dependances, its described as SQLite for Node.  It targeted towards small projects that need a on system database that does not need to be clustered or managed, It takes some API’s and implementation from MongoDB.

However once looking into this I could not see any real life usage for this (for me at least) with the ease of use of MongoDB locally and remotely I would go for Mongo when I need database storage. So onto the point of this post in the comments of the post a lot of people agreed with me but some people mentioned that an easy way to have a persistent data source made totally from JavaScript was to use the var db = Object.create(null);  but looking at this why couldn’t you use the var db = {};.

However after looking into this there are a number of differences between the Object create method and the basic {} method. When using an object as a map you will encounter some issues such as the following:

The reason the above evaluation does not return the expected is due to the prototype methods in JavaScript so in this case the {} object has a toString and hasOwnProperty prototype function so in the off chance but you can’t guarantee some data ended up checking your map for ‘toString’ it might not return the results you expect. the correct method is to use Object.create as this will create an object map with out any prototype functions, as shown below:

Although the above is the best method its always recommended to use a library as this will be fully tested an manage the memory better.



Tags: , , , ,

  • Tilman Potthof

    Interesting point. At first sight I thought this is about the not existing difference between {} and new Object() (often used by Java developers who are new to JS), but Object.create(null) is really different.