background preloader

Language features

Facebook Twitter

Node.js Performance Tip of the Week: Managing Garbage Collection. In our last weekly performance tip, we discussed in detail how the Node.js event-loop works as the orchestrator of requests, events and callbacks.

Node.js Performance Tip of the Week: Managing Garbage Collection

We also troubleshot a blocked event-loop which could wreck havoc on application performance. In this week’s post we’ll dive into the fundamentals of garbage collection (GC) in V8 and how it holds the “keys to the kingdom” in the optimization of Node applications. Checking whether a value is an integer in JavaScript. Integers lead an odd life in JavaScript.

Checking whether a value is an integer in JavaScript

In the ECMAScript specification, they only exist conceptually: All numbers are always floating point and integers are ranges of numbers without decimal fractions (for details, consult “Integers in JavaScript” in “Speaking JavaScript”). In this blog post, I explain how to check whether a value is an integer. ECMAScript 5 There are many ways in which you could implement this check. At this moment, you may want to take a break and try to write your own solution: a function isInteger(x) that returns true if x is an integer and false, otherwise.

Замыкания в Javascript [Часть 1] Перевод статьи Ричарда Корнфорда Javascript Closures.ВведениеРазрешение имен свойств объектов Присваивание значенийЧтение значений Разрешение имен идентификаторов, контексты исполнения и цепь областей видимости Контекст исполненияЦепь областей видимости и свойство [[scope]]Разрешение имен идентификаторов ...

Замыкания в Javascript [Часть 1]

Введение Замыкание Замыкание — это выражение (обычно функция), которое может иметь свободные переменные, вместе со средой, которая привязывает эти переменные (т.е. “замыкает” это выражение). Замыкания относятся к наиболее мощным особенностям ECMAScript (javascript), но они не могут быть применены должным образом без понимания. Самое простое объяснение замыкания в том, что ECMAScript допускает вложенные функции, определения функций и функции-выражения (function expressions) внутри тел других функций. К сожалению, правильное понимание замыканий требует понимания механизмов, которые стоят за ними, и немало технических подробностей.

MountainWest JavaScript 2014 - Error Handling in Node.js by Jamund Ferguson. Array.sort() should not be used to shuffle an array. Array.sort() should not be used to shuffle an array When a developer searches the web for an algorithm to shuffle an array in JavaScript or ActionScript, he will surely find a way to achieve that using the Array.sort() method (see references : JavaScript, ActionScript 3).

Array.sort() should not be used to shuffle an array

While this method is intended to sort an array, it can also be used to randomize it (!). In general, here and there, the following algorithm comes forth : /** * Add a randomize method to the Array object prototype * Usage : * var tmpArray = ["a", "b", "c", "d", "e"]; * tmpArray.randomize(); */ Array.prototype.randomize = function (){ this.sort( function(a,b) { return Math.round( Math.random() * 2 ) - 1; } );}; The principle of this implementation is simple.

Shuffling an array with Array.sort() results in… a not so shuffled array Implementing a shuffle function on arrays, this way, is bad. The result is interesting, isn’t it ? Sharing secrets in Javascript - Binary Fortress. I was wondering if there was a way to share data between instances of a particular javascript constructor, or at least between a limited set of objects, and not to the rest of the application.

Sharing secrets in Javascript - Binary Fortress

In other words, private properties for a single object are easily implemented through the revealing module pattern, but what if you wanted a shared private property between all the instances of a particular constructor? So let’s get to an example. Let’s take a simple constructor Person: function Person(name) { this.name = name; } Now let’s create a context which will hold the private shared data. function Context() { var data = {}; } We want Context to spawn objects that can access its private property data so they all can share the values and objects contained in data.

New Function() Douglas Crockford once said that JavaScript was the only language developers didn't need to learn to use.

new Function()

That's as true a statement as you'll hear when it comes to programming. We all sort of stumbled into JavaScript, mostly due to JavaScript frameworks which made JavaScript magical and easy. "Anonymous function? No idea what that is but the example looked like that so that's what I do. " One thing you may not know about JavaScript functions is that you can pass new Function() the function's body in a string. The JavaScript Here's the basic usage of new Function: var myFunction = new Function('users', 'salary', 'return users * salary'); The last argument to Function is the function body as a string, and the previous arguments represent different arguments to the function.

This" considered harmful (sometimes) Object oriented programming in JavaScript can be complicated, with pitfalls and sneaky details to remember.

this" considered harmful (sometimes)

And while certain aspects usually stay the same, there are as many styles of JavaScript OOP as there are JavaScript frameworks, because there is no official notion of a "class" in JavaScript. Most JavaScript implementations of "classes" do have certain features in common. They rely on the "this" keyword to refer to the current object; after all, it's built into the language. They provide a convenience function to implement subclassing, because it's tricky to get right, especially in older browsers.