Start building your own chatbot now >

As a developer, you might be wondering: should I use a module to do something or should I code this bit of feature by myself? This question leads to many others: how long does it take if I do it myself and is my code more readable? Is it faster to execute?

I have established some benchmarks, which I hope will help you answer these questions.

All tests are done in Javascript, using NodeJs, following this configuration:

Hardware configuration:

  • Computer: Apple Macbook Air (13’) Mid-2013
  • CPU: Intel Core i5 1.3GHz
  • RAM: 4Go 1600 MHz DDR3
  • Storage: Flash PCIe 128Go

Software configuration:

  • OS: OS X 10.11.6
  • iTerm2: 3.0.10
  • zsh: 5.0.8 (x86_64-apple-darwin15.0)
  • NodeJs: 5.0.0
  • npm: 3.3.9

Speed benchmark

In these benchmarks, I’ve compared built-in Javascript functions to module functions. Each test was done 10 times. The results are expressed as an average of these 10 tests, in milliseconds. During the test period, no software was launched on my laptop except for my terminal (iTerm2).

1. Iterate over an array: forEach function – Javascript for vs. Javascript forEach vs. Lodash forEach

All developers use this kind of function: a function that iterates over an array and executes a piece of code for each element. The test just applies the « Math.sqrt() » function, that calculates the square root of a number, to each element in this array.

Lodash is the module this test benchmarks.

Code with Javascript for loop:

var array = []

for (var i = 0; i < 1000000; i++) {
 array[i] = i;
}

var start = new Date()

var l = array.length
for (var j = 0; j < l; j++) {
 Math.sqrt(array[j])
}

var end = new Date()

console.log(end.getTime() - start.getTime() + 'ms')

Code with the Javascript forEach function:

var array = []

for (var i = 0; i < 1000000; i++) {
 array[i] = i;
}

var start = new Date()

array.forEach(elem => Math.sqrt(elem))

var end = new Date()

console.log(end.getTime() - start.getTime() + 'ms')

Code with Lodash forEach function:

var _ = require('lodash')

var array = []

for (var i = 0; i < 1000000; i++) {
 array[i] = i;
}

var start = new Date()

_.forEach(array, elem => Math.sqrt(elem))

var end = new Date()

console.log(end.getTime() - start.getTime() + 'ms')

With a « Javascript for loop » function: 3.3ms

With a « built-in Javascript forEach » function: 50.3ms

With a « Lodash forEach » function : 37.8ms

For this first test, we can see that the « Javascript for loop » is 15 times faster than the « built-in Javascript forEach » and 11 times faster than the « Lodash forEach ». So, the « Lodash forEach » is 1.3 times faster than the « built-in forEach ».

2. Iterate over an array: map function – Javascript map vs. Lodash map

Theses functions look like forEach ones, but are designed to execute only one function per element, and not a piece of code. Again, the test just applies to each element of this array the « Math.sqrt() » function that calculates the square root of a number.

Code with the Javascript map function:

var array = []

for (var i = 0; i < 1000000; i++) {
 array[i] = i;
}

var start = new Date()

array.map(Math.sqrt)

var end = new Date()

console.log(end.getTime() - start.getTime() + 'ms')

Code with the Lodash map function:

var _ = require('lodash')

var array = []

for (var i = 0; i < 1000000; i++) {
 array[i] = i;
}

var start = new Date()

_.map(array, Math.sqrt)

var end = new Date()

console.log(end.getTime() - start.getTime() + 'ms')

With  « built-in Javascript Map » function: 137ms

With « Lodash Map » function: 16.5ms

The « Lodash Map » function is 8.3 times faster than « built-in Javascript Map » function.

3. Filter an array: filter function – Javascript filter vs. Lodash filter

Theses functions only allow you to choose some elements in the array, by filtering them through a determined condition. In this test, we just want to keep even numbers (ie. « number modulo 2 » equals zero).

Code with Javascript filter function:

var array = []

for (var i = 0; i < 1000000; i++) {
 array[i] = i;
}

var start = new Date()

array.filter(elem => elem % 2)

var end = new Date()

console.log(end.getTime() - start.getTime() + 'ms')

Code with Lodash Filter function:

var _ = require('lodash')

var array = []

for (var i = 0; i < 1000000; i++) {
 array[i] = i;
}

var start = new Date()

_.filter(array, elem => elem % 2)

var end = new Date()

console.log(end.getTime() - start.getTime() + 'ms')

With « built-in Javascript filter » function: 139.7ms

With « Lodash filter » function: 34.6ms

The « Lodash filter » function is 4 times faster than the « built-in Javascript filter » function.

4. Get the hour of the current time: Javascript function vs. moment.js function

In this test, we compare the « built-in Javascript Date class » with the moment.js function, to get the the current hour.

Code with the Javascript function:

var start = new Date()

new Date().getHours()

var end = new Date()

console.log(end.getTime() - start.getTime() + 'ms')

Code with moment.js function:

var moment = require('moment')

var start = new Date()

moment().hour()

var end = new Date()

console.log(end.getTime() - start.getTime() + 'ms')

With « built-in Javascript » function: 0.7ms

With « moment.js » function: 2.5ms

The « built-in Javascript » function is 3.5 times faster than the « moment.js » function, because moment.js instantiates its own object to be able to call some functions developers can work with.

5. Get a date in IOS 8601 format: Javascript function vs. moment.js function

Now, we compare speed to get a date in ISO 8601 format. In « built-in Javascript », it uses the toISOString() function. In « moment.js », it uses format() function.

Code with Javascript function:

var start = new Date()

new Date().toISOString()

var end = new Date()

console.log(end.getTime() - start.getTime() + 'ms')

Code with moment.js function:

var moment = require('moment')

var start = new Date()

moment().format()

var end = new Date()

console.log(end.getTime() - start.getTime() + 'ms')

With « built-in Javascript » function: 0.7ms

With « moment.js » function: 5.6ms

The « built-in Javascript » function is 8 times faster than « moment.js » one, for the same reason as the previous test.

6. Make an http request: built-in http NodeJS function vs. request module

This test compares execution time between the http module, provided by default with NodeJS since version 0.3.6, with the request module.

Code with http module (included in NodeJs):

var http = require('http')

var start = new Date()

http.get('http://google.fr', res => {
 var rawData = ''

 res.on('data', (chunk) => rawData += chunk)

 res.on('end', () => {
 var end = new Date()

 console.log(end.getTime() - start.getTime() + 'ms')
 })
})

Code with request module:

var request = require('request')

var start = new Date()

request('http://google.fr', function (error, response, body) {
 var end = new Date()

 console.log(end.getTime() - start.getTime() + 'ms')
})

With  « http NodeJS » function: 50.6ms

With « request module » function: 134ms

The « http NodeJS » function is 2.6 times faster than the « request » function, but there is a reason: « request » module provides a callback function in which body response is already parsed and usable! With the « http NodeJS » module, you have to parse response yourself to access body response. The next test will take parsing into account.

7. Use the Recast.AI API: Use built-in Javascript function or Recast.AI SDK

For this test, we make a request to Recast.AI API, through /v2/request route. The main goal of the code is to get all datetime entities to be displayed, with the console.log() function.

It uses the « built-in NodeJS https module ». Then, it uses JSON.parse() to parse the result returned by Recast.AI API. Then, we check if there is any datetime entity, before displaying them. 

Another way to make a request to Recast.AI is to use our NodeJS SDK to get entities, with the function our team has created.

In both cases, the sentence sent to Recast.AI is « Today, it’s Sunday ». We also provide the language of the sentence, here English, not to be distorted by the auto detection language.

Code with https module (included in NodeJS):

var querystring = require('querystring')
var https = require('https')

var start = new Date()

var req = https.request({
 hostname: 'api.recast.ai',
 path: '/v2/request',
 method: 'POST',
 headers: {
 'Content-Type': 'application/x-www-form-urlencoded',
 'Authorization': 'Token XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
 }
}, res => {
 var rawData = ''

 res.on('data', chunk => {
   rawData += chunk
 })

 res.on('end', () => {
   var json = JSON.parse(rawData)

   if (json.results
   && json.results.entities
   && json.results.entities.datetime) {
     var locations = json.results.entities.datetime

     console.log(locations)
   }

   var end = new Date()
   console.log(end.getTime() - start.getTime() + 'ms')
 })
})

req.write("text=Today it's Sunday&language=en")

req.end()

Code with request module:

var recastai = require('recastai')

var client = new recastai.Client('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'en')

var start = new Date()

client.textRequest('Today, it\'s Sunday')
.then(res => {
 var locations = res.all('datetime')

 console.log(locations)

 var end = new Date()
 console.log(end.getTime() - start.getTime() + 'ms')
}).catch(err => {
 console.log(err)
})

With « built-in Javascript » functions: 1781.7ms

With « Recast.AI NodeJS SDK » functions: 1463.7ms

As you can see, our SDK is 18% faster than basic built-in javascript code. This is because we use modules when we have to, to gain precious milliseconds. It may seem useless to fight against milliseconds, but while your code makes 10 requests to Recast.AI, using SDK instead of « built-in Javascript code » will save you 3 seconds!

Another important thing is readability of your code. Even if using a module is slower than a built-in code, your code doesn’t have to worsen with time. So, maybe using modules is a good thing. Let’s check.

Readability benchmark

Let’s have a look at the last speed benchmark test. As you can see, using Recast.AI SDK makes code 56% lighter than coding this feature by yourself. But the gain is much more than only readability. Our SDK, and modules in general, provide many functions to help you reach your goal.

Using this, rather than your own code, will make your job much more efficient.

If we take a look back to the fourth or fifth speed benchmarks, we can see two pieces of code are pretty equal in readability. But, using the moment.js module is more helpful to manipulate dates and durations rather than “built-in Javascript Date class”.

Just to give you an example, it’s right when you want to add a duration to a date. Here are two pieces of code in which we want to add 1 month to current date and display it in ISO 8601 format:

Code with Javascript class:

var start = new Date()

var date = new Date()

date.setMonth(date.getMonth() + 1)

console.log(date.toISOString())

var end = new Date()

console.log(end.getTime() - start.getTime() + 'ms')

Code with Moment.js module:

var moment = require('moment')

var start = new Date()

var date = moment().add(1, 'month')

console.log(date.format())

var end = new Date()

console.log(end.getTime() - start.getTime() + 'ms')

As we can see, the moment.js code is more understandable than the other. Globally, modules use human readable functions to be easy to use. If, in addition the module documentation is well written, using a module is a real advantage!

Best practices

Depending on what you have to do in your code, using a module can be helpful in your life as a developer.

Some of them are much faster than basic code you can write. And even if they are a bit slower, they allow you to save a lot of time in your job.

marian-01

This is why, at Recast.AI, we provide many SDK, available on our github, in many languages!

It doesn’t matter what language you code in, I strongly recommend you use modules/packages to improve the quality of your code and cut down the time it takes to produce a feature.

Developers, you don’t have to reinvent the wheel 🙂 

Want to build your own conversational bot? Get started with Recast.AI !

Subscribe to our newsletter


There are currently no comments.