The JavaScript blog.


frameworks libraries v8 node testing rest modules

Node Roundup: io.js 2.0, Bootstruct, JSVerify

Posted on .

io.js 2.0

io.js 2.0 was released this week, and it brings some big changes from V8 and fixes to core modules.

The V8 changes include the class keyword in strict mode without flags, shorthand method and property syntax, rest parameters (with --harmony-rest-parameters), computed property names (with --harmony-computed-property-names), and unicode escapes (with --harmony_unicode and --harmony_unicode_regexps). "Rest parameters" means function(a, b, ...args) {}, and args is actually a real array. MDN has more details.

Unfortunately this release has breaking changes, so you'll want to test it thoroughly. The breaking changes are partly related to V8 because the C++ API has changed. I think this might impact some native modules, but I haven't checked the details. The behaviour of os.tmpdir has changed as well, it no longer returns a path with a trailing slash.


Bootstruct (GitHub: taitulism/Bootstruct, License: ISC, npm: bootstruct) by Taitu Lizenbaum is a module for creating RESTful frameworks based on files and folders. If you create a file in a folder called post, it will map the methods inside to POST requests.

To write methods, you have to create functions that take a single argument: io. This value has res and req objects, so it looks similar to other Node HTTP frameworks.

The readme has a lot of details on how Bootstruct generates the internal representation of the folders, filenames, and HTTP verbs, and it seems like it would be fairly easy to get used to the pattern. If you consider app.httpVerb to be unnecessary boilerplate, or perhaps have folders of data that you could map to a Bootstruct application with suitable wrapping code, then you might find it useful.



I'll admit that I was a little bit confused by JSVerify (GitHub: jsverify/jsverify, License: MIT, npm: jsverify). It's a "property-based" testing framework, which to my JavaScript-oriented mind interpreted as something that tests properties on objects. Property-based testing actually refers to a style of testing where properties are specifications of a behaviour that should hold certain values. I found a nice and short definition of it by Jessica Kerr:

Property-based tests make statements about the output of your code based on the input, and these statements are verified for many different possible inputs.

JSVerify will generate inputs for a function. This example is taken from the documentation:

// forall (f: json -> bool, b: bool), f (f (f b)) ≡ f(b).
var boolFnAppliedThrice =  
  jsc.forall('bool -> bool', 'bool', function(f, b) {
    return f(f(f(b))) === f(b);

// OK, passed 100 tests

It also includes examples for testing promises. The way it works with JavaScript's syntax and the compatibility with asynchronous code makes the tests look very succinct, but it will take a bit of practice to get the hang of it.


v8 performance node

V8 Optimization Killers

Posted on .

Bluebird gets a lot of respect for its performance and API style, and reading Optimization killers on the Bluebird wiki reinforced my own inclination to use it over the dozens of other competent promise libraries.

Dug up by diggan on Hacker News, this wiki page explores some ways seemingly innocent JavaScript can cause V8 to avoid optimisation. It contains a code sample that allows you to detect if a function has been optimised, which I've been playing with:

// Function that contains the pattern to be inspected (using with statement)
function codeToTest(a, b) {  
  if (arguments.length < 2) b = 5;

function printStatus(fn) {  
  switch(%GetOptimizationStatus(fn)) {
    case 1: console.log('Function is optimized'); break;
    case 2: console.log('Function is not optimized'); break;
    case 3: console.log('Function is always optimized'); break;
    case 4: console.log('Function is never optimized'); break;
    case 6: console.log('Function is maybe deoptimized'); break;

// Fill type-info
codeToTest(1, 2);

// The next call
codeToTest(1, 2);

// Check

%OptimizeFunctionOnNextCall causes V8 to to check if a function can be optimised, and then marks it for optimisation. Running it and then calling %GetOptimizationStatus will get debugger information so you can see if a given function can be optimised.

To run this example, the Bluebird wiki suggests using the following Node options:

node --trace_opt --trace_deopt --allow-natives-syntax test.js  

The --trace_opt option logs the names of optimised functions, and --trace_deopt logs "deoptimisations". The --allow-natives-syntax option allows you to use the V8 functions that start with a percent, like %OptimizeFunctionOnNextCall.

If this all sounds interesting but you're focused on client-side development, then you might like to look at the Web Tracing Framework from Google. It can use some of V8's extra tracing options, and has both Chrome and Firefox extensions with rich instrumentation features. You'll need to enable some flags to use it.


google v8 embedding Microsoft

Embedded JavaScript: Your Next Career Move

Posted on .

Most of us started out writing JavaScript for web pages. Even if you're a server-side developer focused on Node, you probably wrote client-side JavaScript first. We see JavaScript as a language closely integrated with the DOM, and as a relatively lightweight server-side language, but you can flip this around and think of it as en entirely embedded language: after all, it's embedded within web pages -- it's bridged to native data structures and functionalities that are required by the runtime.

Why Embed?

Why are scripting languages embedded in other software at all? If you think about a large C++, C#, or Objective-C project, then build time and deployment are major issues. Certain programming chores lend themselves well to scripting languages, so rather than rebuilding a huge project every time a small tweak is required, changing a script might be more productive.

In game development Python and Lua are typical choices for scripting. They're used for game interfaces, logic, asset loading, AI, and more. Part of the success of the modding community based around Unreal engine games came from the use of UnrealScript.


You're probably wondering: why Python and Lua? It's mainly because these languages are widely adopted and suitable for embedding. Boost.Python and Luabind are examples of libraries that help with binding. Without bindings to the underlying native code it's impossible for the scripting language to communicate, so this has to be fast and easy.

Web UI

An alternative to low-level binding is to use a web view that hosts JavaScript code, with some event-based bindings to the underlying application. Many mobile and desktop applications use web views for chunks of UI that are easier to implement with HTML, so this is happening more often than you might realise.

Once you've got large amounts of UI written with HTML, the required JavaScript starts to become messy. That's where modern MVC frameworks like Backbone and AngularJS come in: but writing this code properly is a whole skill in itself, so that's when the C++/Objective-C developers call us for help!

Unfortunately, embedding JavaScript as a runtime isn't simple as it should be. There are many different ways to do it for each language.


In the .NET world there are lots of projects for running JavaScript inside your applications. IronJS is an ECMAScript 3.0 implementation built on Microsoft's Dynamic Language Runtime. ClearScript allows multiple scripting languages to be added to .NET applications: it supports JavaScript through V8 -- imagine being able to add using Microsoft.ClearScript.V8 to a C# program and then do engine.Execute("Console.WriteLine(lib.System.DateTime.Now)"). There's also Jurassic -- another ECMAScript runtime.


The general wisdom seems to be that embedding V8 is the way to go. There are also SpiderMonkey-based projects like libjspp.


Apple's Introduction to WebKit Objective-C Programming Guide explains how to access JavaScript from Objective-C.

Another approach is to embed JavaScriptCore. Apparently, Appcelerator Titanium uses JavaScriptCore for iOS projects -- you should be able to figure this out by looking at Appcelerator's GitHub projects.

Job Opportunities

I'm currently contracting at a particularly eclectic office: my team has Objective-C, C++, C#, and JavaScript developers. The shared wisdom amongst the non-JavaScript developers is they want to embed JavaScript in their projects: whether they're games, desktop software, or mobile applications.

That's great for us, but it's worth remembering that JavaScript isn't good at everything -- the type system might not suit certain projects, its scope and coercion rules can be a source of frustration for people from other backgrounds. It looks C-like, but that doesn't mean it's C: and that's the problem when you're working with people who are more fluent in C than JavaScript.

If you're wondering how people see JavaScript from the other side, then I found Choosing a scripting language by David Rosen interesting: he struggled with JavaScript's readability because he couldn't overload operators, and wanted to write maintainable linear algebra code.

However, it seems like there's a huge amount of momentum behind JavaScript in a growing number of platforms, so it's an area you should take seriously. Don't be surprised to find yourself writing scripts for games or apps in the near future.


tutorials v8 extjs sencha

Using SilkJS ORM and ExtJS Grids Together

Posted on .

Mike Schwartz has been developing software since the early 1970s. He's proficient in JavaScript, PHP, C/C++, Java, C#, Perl, and Assembler language. He founded Best Internet Communications, the SF Bay Area's largest ISP through the late 1990s. He currently works for PINT, Inc, the longest-established San Diego firm specializing in Web site design and Web application development. His focus for the past several years has been in writing front facing and back end JavaScript, especially ExtJS and Helma. He recently released SilkJS, a very fast WWW server written almost entirely in JavaScript.

SilkJS is a very fast JavaScript based HTTP server that was designed to work with ExtJS. All code for the server is written in JavaScript, including almost the entirety of the server itself. You can check out the server's GitHub repository at mschwartz / SilkJS and the repository for this demo at mschwartz / SilkJS-ext.

The SilkJS Install.js script creates a /usr/share/SilkJS directory with the HTTP server, library files, and default documentRoot. It also installs the main HTTP server in /usr/local/bin/httpd-silk.js.

Let's examine the SilkJS-ext repository first. This is config.js:

Config.mysql = {  
  host: 'localhost',
  user: 'mschwartz',
  passwd: '',
  db: 'ext3'
Config.documentRoot = 'docroot';  

This file overrides the default Config object, specifying MySQL connection information, and a documentRoot for this example.

The actual Server-Side JavaScript code is implemented in under 60 lines of code, in Server.js:

SQL = new MySQL();  

  name: 'Users',
  fields: [
    { name: 'userId', type: 'int', autoIncrement: true, defaultValue: 0 },
    { name: 'username', type: 'varchar', size: 64, header: 'User Name', width: 128, autoExpand: true, editable: true },
    { name: 'password', type: 'varchar', size: 64, header: 'Password', serverOnly: true, editable: true },
    { name: 'email', type: 'varchar', size: 128, header: 'E-Mail Address', width: 128, editable: true },
    { name: 'created', type: 'int', defaultValue: function() { return parseInt(new Date().getTime() / 1000, 10); }, header: 'Created', width: 150, format: 'DateTime', editable: false }
  primaryKey: 'userId',
  indexes: [

function main_action() {  
    '<!doctype html>',
    ' <head>',
    '   <title>Schema / ExtJS Demo</title>',
    '   <link rel="stylesheet" type="text/css" href="/ext-3.4.0/resources/css/ext-all.css" />',
    ' </head>',
    ' <body>',
    ' <script type="text/javascript" src="/ext-3.4.0/adapter/ext/ext-base.js"></script>',
    ' <script type="text/javascript" src="/ext-3.4.0/ext-all-debug.js"></script>',
    ' <script type="text/javascript" src="/client/Ext.ux.SchemaGrid.js"></script>',
    ' <script type="text/javascript" src="/client/ViewPort.js"></script>',
    ' <script type="text/javascript">',
    '   Schemas = ' + Json.encode(Schema.getSchemaExtJs()) + ';',
    ' </script>',
    ' </body>',
function Server_action() {  
  switch (req.data.method) {
    case 'listUsers':
      Json.success(Schema.list('Users', {}, function(o) {
        o = Schema.clean(o);
    case 'editUser':
      var example = Json.decode(req.data.example);
      example.userId = example.userId || 0;
      Schema.putOne('Users', example);
    case 'deleteUsers':
      var examples = Json.decode(req.data.examples);
      forEach(examples, function(example) {

The first thing it does is create a global MySQL object, named SQL. This SQL object is used by the ORM to generate queries. With the ORM, you rarely have to generate any queries yourself, and even then, the ORM does most of the work.

The Schema.add() method is called to install a "Schema" in the ORM. The Schema is defined as a JavaScript object. The definition above shows only a part of what's possible. The name member is the name of the table in the database. The fields array is an array of field definitions. The primaryKey member is the primary key of the table, and the indexes array is an array of additional indexes to be created for the table.

For the fields array, each item is an object with a few required members and some optional ones. The required ones are name, and type. If type is 'varchar', then size is also required. Only one of the fields may be autoIncrement: true, and is typically the primaryKey of the table as well. This autoIncrement field is also typically the ID field of ExtJS DataStores on the client-side.

The defaultValue member may be an expression or a function. The defaultValue member is used to set a default value for the field when creating a new entry in the database. As you can see in the example above, we have a 'created' field of type 'int' (a Unix-style timestamp) that has a defaultValue of a function that creates a Unix timestamp for the current time.

One additional members may be specified. Note the password field is marked serverOnly: true -- this is to specify that the value of this field should not be sent to the client (we'll see this later).

Currently, every other member of a field definition is ignored by the ORM, and can be used for your application for whatever purposes you like. In this case, I specify information that will help us generate ExtJS Grid columns, and form fields for editing records. For example, username is autoExpand: true, and fields with header: 'some string' will show in the grids.

The SilkJS HTTP server will call main_action() (if it is defined) if / is the specified URL. Here we simply write a typical HTML document to the browser. The output HTML looks like this:

<!doctype html>  
    <title>Schema / ExtJS Demo</title>
    <link rel="stylesheet" type="text/css" href="/ext-3.4.0/resources/css/ext-all.css" />
    <script type="text/javascript" src="/ext-3.4.0/adapter/ext/ext-base.js"></script>
    <script type="text/javascript" src="/ext-3.4.0/ext-all-debug.js"></script>
    <script type="text/javascript" src="/client/Ext.ux.SchemaGrid.js"></script>
    <script type="text/javascript" src="/client/ViewPort.js"></script>
    <script type="text/javascript">
    Schemas = {"Users":{"name":"Users","fields":[{"name":"userId","type":"int","autoIncrement":true,"defaultValue":0},{"name":"username","size":64,"header":"User Name","width":128,"autoExpand":true,"editable":true},{"name":"password","size":64,"header":"Password","serverOnly":true,"editable":true},{"name":"email","size":128,"header":"E-Mail Address","width":128,"editable":true},{"name":"created","type":"int","header":"Created","width":150,"format":"DateTime","editable":false}],"primaryKey":"userId"}};

All records in SilkJS and ExtJS are effectively just JavaScript objects; each member is a column/value pair from the database. But not every field from the database must exist in these objects! A partial record of this form is what I call an "example." If you call Schema.find(example), it will do a SELECT query based upon the fields you do specify in the example. For example, Schema.find({ username: '%msch%' }) will return an array of records from the database that have a username with 'msch' somewhere in it. Schema.putOne({ username: 'mschwartz' }) will store a new record in the database with defaultValues for the other fields and username set to 'mschwartz'. The only way I can explain examples is with examples!

So editUser expects the client to build an example and send it over. If editing an existing record, the example will have a userId, and the record will be updated by Schema.putOne(). If adding a new record, the example will have no userId or a userId set to 0.

The deleteUsers method expects an array of examples. It calls Schema.remove(example) on each element of the array, which deletes the specified rows from the database. Schema.remove() can be dangerous if you're not careful about the examples you give it. In this case, the array of examples would be some multiple selections on the grid on the client side.

The client-side ExtJS code is in docroot/client and accessed via /client/whatever paths in the HTML. In this case, we have just two files, Viewport.js and Ext.ux.SchemaGrid.js.

ViewPort.js is rather simple:

Ext.onReady(function() {  
  var items = [];
  for (var i in Schemas) {
    var schema = Schemas[i];
      xtype: 'ext-ux-schemagrid',
      title: i,
      schema: schema,
      method: 'list'+i

  new Ext.Viewport({
    layout: 'fit',
    items: [
        xtype: 'tabpanel',
        activeTab: 0,
        items: items

It loops through the global Schemas and creates an items array of Panel configs, one Schema Grid per panel. The Ext.ViewPort is simply an Ext.TabPanel with one tab per panel.

The real work on the client is done by Ext.ux.SchemaGrid.js.

I don't want to go into great detail about how this custom component works, but I will explain some of the things it does. Basic ExtJS knowledge is assumed here.

SchemaGrid extends Ext.grid.GridPanel and takes as additional configuration options a Schema, and a method. Those are set in the ViewPort.js code.

In the initComponent method, the URL of the JsonStore is set to /Server (so Server_action() is called on the SilkJS side), and baseParams method is set to the SchemaGrid's config.method, listUsers in our case. The JsonStore's fields are directly used from the Schema's fields array (no muss, no fuss).

After setting up the SelectionModel, the code loops through the Schema's fields and generates column definitions for the grid. Those fields that have a header member and are not serverOnly will have columns displayed. The renderer for each column is a single method in the SchemaGrid class. The default action for our renderer is to render the created/timestamp in human readable format. An autoExpand column may be specified in the Schema definition, and it is handled automatically by this code.

When the JsonStore is loaded or the user clicks on items in the grid, the updateToolbar() method is called to conditionally enable and disable the edit and delete buttons. Basically, you can only edit if one row is selected, and you can't delete unless at least one row is selected.

The editRecord() method dynamically creates a form in a modal dialog window. If a record is passed in, the dialog title says "Edit record" otherwise "Add record." The dialog/form can be used to add new records (click the add button) or edit existing records (select a record, click edit button).

When the OK button is clicked on the dialog, the record (EXAMPLE) is updated with the form fields' values and the Ext.Ajax.request() made to call editUser to update the database.

The deleteRecords() method is called to delete one or more selected records in the grid. It creates the array of examples and calls Ext.Ajax.request() to invoke deleteUsers on the server side.

So let's see it in action!


This is the application when it first starts:

SilkJS/ExtJS application starts

Now the add button has been clicked:

SilkJS/ExtJS button click

Here I've added a few records:

SilkJS/ExtJS records added

This is what happens when a record is opened for editing:

SilkJS/ExtJS editing record


So, what's neat about this?

If you edit the Schema definition at the top of Server.js, those changes will immediately be reflected in the database when you either restart the server or the browser makes a new request (reload the browser, click the reload button in the paging toolbar).

What do I mean? If you change the size of password field to 128, the ORM will submit the appropriate ALTER TABLE statements to MySQL to change the size of the field. If you add a field to the fields array, it will be added to the table in the database as well. If you delete a field, it will be deleted in the database. If you rename a field, it will be renamed in the database.

If you add a second Schema definition at the top of Server.js, the table will be created next access. If you reload your browser, you will see two tabs, one for each table, and the CRUD operations apply to both.

Outside of the Schema definitions, which are compact in their own right, and the main_action() method that spews out the HTML for the app, the entire CRUD operations on the server side consist of under 20 lines of code.

The entire demo took me under 1 hour to create, all but 2-3 minutes was spent writing the client-side code.


v8 redis node modules iOS

Node Roundup: Nedis, Redisify, Node on iOS

Posted on .

Node v0.4.6

Node v0.4.6 was released last week. v8
has been updated to, which reminded me -- you can track v8
releases at Google Code's v8
. Amongst
other things there's a full v8

which may be of interest.


Nedis (MIT License, npm: nedis) by TJ Holowaychuk is a Redis server implemented with Node. TJ has posted some benchmarks of it:

nedis: ops 25048, per second 5009.6
redis: ops 54850, per second 10970

nedis: ops 32729, per second 6545.8
redis: ops 54714, per second 10942.8

... although he says he built this for fun. He's written a blog post with more details: Redis Implemented With

and mentions the background of the project:

[...] however as our team grows larger, and as we add more non-technical team members over at LearnBoost I figured it would be nice help prevent the need for compiling development dependencies.


Redisify (MIT License, npm: redisify) by Jonah Fox adds Redis capaibilites to objects:

var User = {
  key: "Users"

User.redis = redisify(client);

User.redis('get', 'xx', function(val) {
  // redis "get Users:xx"
  // User == this

This method could actually be named anything -- one of Jonah's examples
refers to it as db which reads well. He's written a blog post about
the library: Redisfy your

Node on iOS

I saw this blog post by Nathan Rajlich called NodeJS on
. He's started
node-iOS which is an effort to bring native iOS bindings to Node. You'll require a jailbroken iPhone
to use this, but as someone who does a fair bit of Objective-C I find
this very interesting.