Posts

Showing posts from January, 2014

Win a ticket for Fluent 2014

Update 2014-02-07: A winner has been determined and received the ticket. If you didn’t win: get a 20% discount on a Fluent ticket via the code 2ALITY20 The Fluent 2014 conference takes place in San Francisco from March 11–13. Its tag line is “The Web Platform”. The 2ality blog raffles off a ticket for the last two days (excluding the workshop day). To enter the raffle, tweet the following text: I’d like to win a ticket for Fluent 2014. #2alityFluent2014 http://www.2ality.com/2014/02/fluent-2014-raffle.html The deadline is Friday, February 7, 2014, 18:00 GMT I’ll contact the winner via Twitter, within a few hours of the deadline. (If the winner isn’t following me already, I’ll ask them to temporarily do so, so that I can send them a direct message.) [Legal disclaimer: I make no guarantees w.r.t. to there being a winner. Contesting this raffle via legal means is not allowed.]

Repeating strings efficiently

Recently, Michael Ficarra pointed me to an efficient algorithm for repeating a string several times. In this blog post, I explain how it works. The algorithm makes use of the fact that natural numbers (non-negative integers) can be represented as sums of powers of two. Natural numbers as sums of powers of two You can express any natural number as a sum of powers of two. In fact, that’s what a number in binary notation is. The value of a sequence of n binary digits d i d n-1 d n-2 ... d 0 is the sum ∑ 0 ≤ i < n d i × 2 i = d 0 × 1 + d 1 × 2 + d 2 × 4 + ... Each digit d i is either one or zero. Therefore, each addend is either a power of two or it is zero. The algorithm Roughly, the algorithm iterates over the digits starting with d 0 . After each iteration, it doubles str , the string to repeat. If the current digit is one then the current value of str is added to the result. In other words, if the digit d i is one, 2 i times str is added to the result. The

Binary bitwise operators in JavaScript

This blog post describes JavaScript’s binary bitwise operators: bitwise And ( & ), bitwise Or ( | ), and bitwise Xor ( ^ ). These operators work with 32-bit integers. That is, they convert their operands to 32-bit integers and produce a result that is a 32-bit integer (encoded as a floating point number, at least externally). Inputting and outputting binary numbers In the following examples, we work with binary numbers, via the following two operations: parseInt(str, 2) parses a string str in binary notation (base 2). For example: > parseInt('110', 2) 6 num.toString(2) converts the number num to a string in binary notation. For example: > 6..toString(2) '110' Binary bitwise operators Bitwise And: number1 & number2 > (parseInt('11001010', 2) & parseInt('1111', 2)).toString(2) '1010' Bitwise Or: number1 | number2 > (parseInt('11001010', 2) | parseInt('1111', 2)).toString(2)

The new operator implemented in JavaScript

Code snippet – this is roughly how you would implement the new operator in JavaScript: function newOperator(Constr, args) { var thisValue = Object.create(Constr.prototype); // (1) var result = Constr.apply(thisValue, args); if (typeof result === 'object' && result !== null) { return result; // (2) } return thisValue; } Two things are noteworthy: Line 1: The prototype of a new instance of a constructor Constr is Constr.prototype [4] Line 2: The implementor of a constructor can override that the new operators returns this , by returning an object. This is useful when a constructor should return an instance of a sub-constructor. The blog post [3] gives an example . Further reading: JavaScript inheritance by example In defense of JavaScript’s constructors [Why I recommend constructors, even though they are far from perfect] Exemplar patterns in JavaScript [alternatives to constru

ECMAScript 6: TC39 meetings, March–November 2013

TC39 [1] is the committe that currently plans ECMAScript 6 (code-named ECMAScript.next), the next version of the JavaScript language standard. In this blog post, I’m summarizing the highlights of several meetings that they had in 2013, in March, May, July, September and November. Previous blog posts summarized prior meetings. This post is made possible by Rick Waldron’s excellent notes of the meetings. March Adobe joins TC39 ( announcement ). Iterator protocol: TC39 decided on an iteration protocol for ECMAScript 6, which is described in [2] . new Date(dateObj) creates a clone of dateObj . Two new array methods proposed by Rick Waldron: Array.prototype.find(predicate, thisArg?) Array.prototype.findIndex(predicate, thisArg?) May (All highlights subsumed by more recent explanations below.) July Maps can optionally be initialized via an iterable [2] of [key,value] pairs. Two new arra

ECMAScript 6: merging objects via Object.assign()

New version of this blog post: inside “ ECMAScript 6: new OOP features besides classes ”. Copying all properties of one object to another one is a common operation in JavaScript. This blog post explains ECMAScript 6’s implementation of it, which is called Object.assign() . This merging operation has a name in the JavaScript ecosystem that is only used there (and, unfortunately, clashes with classic OOP terminology): “extend”. Two examples of “extend” being provided by libraries: Prototype: Object.extend(destination, source) Prototype first used the name “extend” for this operation. Underscore.js: _.extend(destination, *sources) Object.assign() For merging objects, ECMAScript 6 will have the function Object.assign(target, source_1, ..., source_n) This function modifies target and returns it: it first copies all enumerable [1] own operties of source_1 to target , then those of source_2 , etc. Property keys: either strings or symbols In ECMAScript 6, proper

Evaluating JavaScript code via eval() and new Function()

This blog post examines how one can dynamically evaluate code in JavaScript. eval() eval(str) evaluates the JavaScript code in str . For example: > var a = 12; > eval('a + 5') 17 Note that eval() parses in statement context [1] : > eval('{ foo: 123 }') // code block 123 > eval('({ foo: 123 })') // object literal { foo: 123 } eval() in strict mode For eval() , you really should use strict mode [2] . In sloppy mode, evaluated code can create local variables in the surrounding scope: function f() { eval('var foo = 1'); console.log(foo); // 1 } That isn’t possible in strict mode. However, evaluated code still has read and write access to variables in surrounding scopes. To prevent such access, you need to call eval() indirectly. Indirect eval() evaluates in global scope There are two ways to invoke eval() : Directly : via a direct call to a function whose name is “eval”. In

Web platform: five technologies to look forward to in 2014

This blog post describes five technologies that will make 2014 an exciting year for the web platform: asm.js: near-native performance on the web ParallelJS: parallelized JavaScript code ECMAScript 6 (ES6): evolving the language, uniting the community Web Components: a standard infrastructure for widgets CSS Grid Layout: native-like GUI layout asm.js: near-native performance on the web What it is: a subset of JavaScript that runs fast on current engines (about 70% of compiled C++ code, at the moment). Why it is exciting: Near-native speed in web browsers. Tightly integrated with JavaScript. Already compatible with all existing JavaScript engines. An engine optimizing asm.js code takes more work, but can be implemented incrementally. More information: “ asm.js: closing the gap between JavaScript and native ” ParallelJS: parallelized JavaScript code What it is: This project was born under the name “River Trail” and parallelizes JavaScript code that uses