JavaScript

JavaScript performance library simpleperfo.js

January 30, 2016 JavaScript, Libraries & Frameworks 4 comments

JavaScript performance library

Learn how to measure your JavaScript performance with my effective and super simple in use library. Simpleperfo.js is a JavaScript performance library that allows to measure execution times of your functions in order to allow you to optimize them effectively.

The library is very simple in use. My intention was to provide a library that will be easy to use even for JavaScript beginners. There are no difficult configurations or complicated APIs. Simply load the library to your existing project and use one of the available functions.

Below is an example code to measure two functions calculating Fibonacci numbers. Checking which one is faster is just matter of a few lines of simple code!

/* Algorithms to tests */
function fiboIterative(no) {
if(no<=2) { return 1; }
var a=1, b=1, sum=0;
for (var i=3; i<=no; i++) {
sum = a+b;
a = b;
b = sum;
}
return sum;
}

function fiboRecursive(no) {
if(no<=2) {
return 1;
} else {
return fiboRecursive(no-2) + fiboRecursive(no-1);
}
}

/* Measuring execution time */
var result1 = simpleperfo.executionTime(fiboIterative,[15]);
var result2 = simpleperfo.executionTime(fiboRecursive,[15]);

console.log("fiboIterative(15): " + result1 + "ms");
console.log("fiboRecursive(15): " + result2 + "ms");

This JavaScript performance library is available for download on simpleperfo.js on GitHub

 

Prototypes in Javascript

March 23, 2015 JavaScript 5 comments

JavaScript performance library

This article is a crash course of JavaScript prototypes.

What are prototypes in JavaScript ?

Prototype is an object from which other objects inherit their’s properties. You can think about a prototype object as a base object used to create final objects.

As an example, you can imagine a prototype object representing a generic car. This prototype has all of the elements that every car has, ie. engine, steering wheel, gearbox, brakes, etc. You can use this prototype to build more specific cars (objects) by extending the prototype’s properties. In example, you can insert to your newly built car (object) a faster engine and a lower suspension to create a sport car. You can add leather seats and other features to create a limousine. Instead of creating engine, brakes, and other base elements every time you create a new car (object), you can simply inherit those fundamental properties from a prototype.

Every object in JavaScript has it’s prototype (prototypes in JavaScript can have their own prototypes as well). The main, fundamental prototype, which is the core of all objects existing in JavaScript, is Object.prototype.

All objects created by object literal or with new Object() are directly linked to Object.prototype.

Some objects are not directly linked to Object.prototype. In example, an object created with new Date() is a prototype of Date.prototype (which is a prototype of Object.prototype). Beside this example, there are much more standard prototypes in JavaScript. (more…)

JavaScript array methods

February 16, 2015 JavaScript 2 comments

JavaScript performance library

Quick explanation of JavaScript Array methods:

array.concat
array.join
array.push
array.pop
array.shift
array.unshift
array.slice
array.splice
array.reverse
array.sort (more…)

Cascades in JavaScript

February 7, 2015 JavaScript 1 comment

JavaScript performance library

Concept of cascades in JavaScript is very simple. Take a look at the following code:

cascade.
    setManufacturer("Hyundai").
    setModel("Coupe").
    setColor("Red").
    setEngine("2.7 v6").
    describe();

Notice how each of the methods are executed. Every of those methods does not to have to return anything specific. If we have those methods return this, we can create cascades. In a cascade, we can invoke multiple methods on the same object in sequence in a single statement.

Learn how to create cascades in JavaScript by reading this article. (more…)

Closures in JavaScript

February 7, 2015 JavaScript 2 comments

JavaScript performance library

What are closures?

By quoting Wikipedia:

In programming languages, a closure (also lexical closure or function closure) is a function or reference to a function together with a referencing environment—a table storing a reference to each of the non-local variables (also called free variables or upvalues) of that function. A closure—unlike a plain function pointer—enables a function to access those non-local variables even when invoked outside its immediate lexical scope.

The above description of closure may sound a little difficult at first. In fact it is a very simple concept and you will learn it very fast. (more…)

Exceptions in JavaScript

February 4, 2015 JavaScript 2 comments

JavaScript performance library

Learn how to work with exceptions in JavaScript in this short tutorial.

Exceptions in JavaScript have a very similar syntax as in other languages with C-like syntax:

try {
    throw new Error("Error happened inside 'try' block!");
} catch(e) {
    alert(e.message); // = Error happened inside 'try' block!
} finally {
    alert("Finally block"); // Executed no matter if an exception was thrown or not
}

(more…)

JavaScript this keyword in functions

February 3, 2015 JavaScript 1 comment

JavaScript performance library

In JavaScript, this keyword always depends on how given function is invoked. There are four patterns to invocate a JavaScript function. Understanding those patterns is important knowledge to understand how JavaScript this keyword works.

Learn JavaScript this keyword by reading this article. (more…)