Afshin Mehrabani

Programmer, open-source lover. Creator of IntroJS, PACKT publishing author and technical editor.

Read this first

Python: List and Tuple performance benchmark

Suppose you have two options to implement a solution using a programming language, what are important factors to select one of options? I do believe one of concerns for a programmer would be performance benchmark between those options.

In this short blog post I’d like to share my simple code and results for performance benchmark between Python list and tuple. Two features to create a list, but with this difference, that tuples are immutable and you can’t alter them after initializing.

Following code shows a simple usage of list and tuple to create a series of items:

# this is a list, you can alter it in next lines l = [1, 2, 3, 4, 5] # this is a tuple and it's immutable t = (1, 2, 3, 4, 5) 

Please note that you can store different data types as an item for both tuple and list.

My scenario to make a performance benchmark between list and tuple is retrieving 2,000,000 ...

Continue reading →


Using Redis as session store for ExpressJS + PassportJS settings

Since all other articles about using Redis as a session store for ExpressJS are out of date due to latest update of connect-redis, I’m going to write this short article to show how to use Redis as session store for ExpressJS.

In order to use Redis as session store for ExpressJS, first of all install connect-redis package using npm:

npm install connect-redis 

Then, add connect-redis to your dependencies and Following code shows the content of app.js file:

var express = require('express'); var RedisStore = require('connect-redis')(express.session); app.use(express.session({ store: new RedisStore({ host: '127.0.0.1', port: 6379 }), secret: 'hey you' })); 

If you’re using PassportJS for users authentication, you can simply add two following lines to your app.js file to enable PassportJS as well:

app.use(passport.initialize());...

Continue reading →


Why C# is not a good choice for web development?

C# is the main programming language in Iran. I’ve worked with several teams, various projects and developers with different development skills. Earlier I’ve worked with PHP.

There weren’t any motivations to migrate from PHP to C# but the company’s infrastructure. Most of web development companies in Iran work with C# and Microsoft technologies, and here is the reason, because they don’t want to learn more. Companies prefer to stay at the same level and do not pay for improving their developers skills!

If you ask them: “Why you are using C# for web development?” I bet they won’t give you acceptable answer.

I haven’t used C# for my own projects, and I won’t at all. It’s preferable to me to use NodeJS or Python, not only because of their popularity, but because they are scripting languages.

After spending ages with C#-based web apps I want to tell...

Continue reading →


How to fix `setImmediate is not defined` error in NodeJs

The setImmediate function introduced in NodeJS v0.10.x to queue functions. It works like process.nextTick function but with a difference in functionality. Read JohnnyHK’s answer to figure out: http://stackoverflow.com/a/15349865/375966

Most of the time, by upgrading the NodeJS core to v0.10.x it can simply solve your problem.

tldr; Upgrade your NodeJS to v0.10.x.

Continue reading →


Become a CreateJS Ninja with “Getting Started with CreateJS” book

Nowadays using HTML5 technologies to develop web pages are become more commonplace and consequently HTML5-based libraries are become more popular. CreateJS is one of popular and successful libraries for creating rich HTML5-based web pages.

What is CreateJS?

A suite of Javascript libraries & tools for building rich, interactive experiences with HTML5.

CreateJS consist of four different libraries and each one is responsible for specific part of HTML5 technology.

  • EaselJS
  • TweenJS
  • PreloadJS
  • SoundJS

With combination of all parts you can develop a rich web application easily.

Getting Started with CreateJS

If you want to start learning CreateJS as fast as possible, “Getting Started with CreateJS” book with Packt Publishing is a good starting point. It’s a step-by-step tutorial for all CreateJS subsets. The book consists of many practical examples that help you to...

Continue reading →


MongoDB singleton connection in NodeJs

In this post, I want to share a piece of useful source code to make a singleton connection in NodeJs. By using this piece of code, you will have always one connection in your NodeJs application, so it will be more faster. Also, if you are using NodeJs frameworks like ExpressJs, it will be useful too.

connection.js:

var Db = require('mongodb').Db; var Connection = require('mongodb').Connection; var Server = require('mongodb').Server; //the MongoDB connection var connectionInstance; module.exports = function(callback) { //if already we have a connection, don't connect to database again if (connectionInstance) { callback(connectionInstance); return; } var db = new Db('your-db', new Server("127.0.0.1", Connection.DEFAULT_PORT, { auto_reconnect: true })); db.open(function(error, databaseConnection) { if (error) throw new Error(error);...

Continue reading →


Simplified MapReduce

I believe one of the best solutions to solve a programming problem is to find a paper or article and read it as a clue. Well, of course Wikipedia, BMI and other sources are really helpful but somehow reading them is a nightmare for me, because of complexity of explanation. Thus, It’s preferable for me to read a straightforward article to find the clue.

You have a problem you find a paper about it. Now you have one and a half problems. Understanding the paper, and implementing it.

— Amir Mohammad Saied (@gluegadget) February 6, 2014

And now, I want to straightforwardly describe one of usable algorithms, its called MapReduce. Perhaps you have heard that before in Hadoop, MongoDB or NoSQL discussions.

Here is an introduction to what is a MapReduce is:

MapReduce is a programming model for processing large data sets with a parallel, distributed algorithm on a cluster.

From:...

Continue reading →


Programming languages war

So frequently I hear a tedious conversation between my colleagues, “PHP is better than foo”, “.NET is better than boo” etc. etc. Each time I hear this sort of dialog I try to ask the reasons of this comparison but till now, no one has given a proper answer since somehow it’s impossible.

Up till now, I’ve coded with JavaScript, PHP, C# and little bit Python. I’m still a newbie in programming industry, but at least I had a lot of mutual projects with some experts. The result of being a part of those projects, taught me how to tackle a problem, how to choose an adequate tools or language and prepare the environment to solve it. Almost in all cases, indicating the programming language wasn’t the bottleneck. However, we did that with considering the problem’s parameters. Design a good architecture and implementing it correctly was the main goal in our projects.

I’d like to...

Continue reading →


Async vs. Sync I/O benchmark in NodeJs

As you know, NodeJs is a non-blocking I/O platform which gives you ability to do non-blocking and event-based functionalities. It has async methods for I/O but it also provide sync version of that methods as well. It means you can write to a file with async/non-blocking methods and you can do the same with sync methods.

So, in this post I want to show you the different between using async or non-blocking I/O and sync I/O. Here I have a HTTP server which has a simple functionality, it just reads a static file from disk and gives the content of file to the user by an HTTP request. There’s two different ways for reading a file from disk in NodeJs, with fs.open (async) or fs.openSync (sync).

Results speak for themselves, as expected. When we try to read a file with Async mode, all steps of reading a file (stat, open, read, close) are async and it means the reading process will not block...

Continue reading →


Migrating Git Repositories

Moving a Git repository from a server to another is a common situation that all developers could face with it, for example moving repositories from Bitbucket to Github or vice versa.

Doing this task is really terrible when you have to move a lot of repositories, you should manually clone it and then push it to the target server. Boring.

I wrote a shell script which help you out to move the repositories from any Git server to another, you can simply configure it and then just hit the Enter.

Configuration

Ok, to get started you should clone or download this repository from Github: https://github.com/afshinm/git-migrate

After that, you should find two files, migrate.sh and CONFIG. We need CONFIG file to configure our migration, it contains from and to servers.

Our CONFIG file is look like this:

repoName1:fromServer1:toServer1 repoName2:fromServer2:toServer2...

Continue reading →