background preloader

Under the hood

Facebook Twitter

Erik's weblog » Blog Archive » Object.prototype is verboten. Feature Detection: State of the Art Browser Scripting. The goal of feature detection in browser scripting with JavaScript is straight forward. Before our code uses an object or object reference we want to know that our use of it will execute without error and behavior as we are expecting. If we don't know these things before we use the object or object reference then we don't want to use it because using it would risk a broken page and ultimately an unsatisfactory user experience. The idea of feature detection has been around for many years. If you are writing a script for the general web, where anyone with a web browser can visit your page then you will likely need to spend a decent amount of time thinking about how the feature detection works and only add progressive enhancements to your page when you know those enhancements will work.

Good feature detection is one of the fundamental indicators of a quality browser script...but developers still aren't using it! Look in all the mainstream JavaScript libraries. Native and Host Objects . `instanceof` considered harmful (or how to write a robust `isArray`) ← back 936 words 10 January 2009 Checking types in Javascript is well known as a pretty unreliable process. Good old typeof operator is often useless when it comes to certain types of values: typeof null; // "object" typeof []; // "object" People often expect to see something like “null” in the former check and something like “array” in the latter one. Fortunately, checking for null is not that hard, despite useless typeof, and is usually accomplished by strict-comparing value to null: value === null; Checking for arrays, on the other hand, is a somewhat tricky business. Obviously, both ways have their pros and cons. 1) `instanceof` operator / `constructor` property instanceof operator essentially checks whether anything from left-hand object’s prototype chain is the same object as what’s referenced by prototype property of right-hand object.

Var arr = []; arr instanceof Array; // true var arr = []; arr.constructor == Array; // true An excerpt from jQuery (rev. 5917): 2) Duck-typing. JavaScript engine. History[edit] Before the second browser war in 2008-2009, the JavaScript engine (also termed JavaScript interpreter or JavaScript implementation) was known as simply an interpreter that read and executed JavaScript source code. By far the most common host environment for JavaScript is a web browser. Web browsers typically use the public application programming interface (API) to create "host objects" responsible for reflecting the Document Object Model (DOM) into JavaScript.

The web server is another common application of the engine. A JavaScript web server exposes host objects representing an HTTP request and response objects, which a JavaScript program then manipulates to dynamically generate web pages. Microsoft's ASP technology for IIS allows server-side code to be written in VBScript or JScript (Microsoft's implementation of JavaScript).

Performance evolution[edit] The JavaScript engine race: 2008 and 2009[edit] The JavaScript engine race: 2010[edit] 2011[edit] JavaScript engines[edit]

Engines

Just-in-time compilation. Applications[edit] A common implementation of JIT compilation is to first have AOT compilation to bytecode (virtual machine code), known as bytecode compilation, and then have JIT compilation to machine code (dynamic compilation), rather than interpretation of the bytecode. This improves the runtime performance compared to interpretation, at the cost of lag due to compilation. JIT compilers translate continuously, as with interpreters, but caching of compiled code minimizes lag on future execution of the same code during a given run. Since only part of the program is compiled, there is significantly less lag than if the entire program were compiled prior to execution. Overview[edit] In a bytecode-compiled system, source code is translated to an intermediate representation known as bytecode.

In contrast, a traditional interpreted virtual machine will simply interpret the bytecode, generally with much lower performance. JIT code generally offers far better performance than interpreters.