Asm. js performance approaches native code

Source: Internet
Author: User
Tags ffi

Asm. js is a high-performance subset of JavaScript, which simplifies features and facilitates optimization. Mozilla now announces that the performance of asm. js is further closer to that of native. The speed of asm. js has reached 2/3 of that of native code, and it can only reach 1/2 or even lower in the past. Mozilla improves the performance of asm. js by changing the processing method of floating point algorithms.

By default, JavaScript uses the float64 data type to provide the maximum data precision. However, compared with int, int32, float, and float32 data types with a smaller range, float64 is less efficient, mozilla SpiderMonkey engine in asm. the float32 data type is added to js, which allows direct translation of the float32 algorithm in the C/C ++ program to asm. js float32 algorithm.

Mozilla believes that the performance of asm. js can be further improved.

Asm. js is an underlying compiler optimized for the JavaScript subset. This is a Mozilla research project, similar to Emscripten, Mandreel, and LLJS.

Sample Code:

 
 
  1. function mymodule(global, foreign, buffer) { 
  2.     "use asm"; 
  3.  
  4.     // ------------------------------------------------------------------------- 
  5.     // SECTION 1: imports 
  6.  
  7.     var H32 = new global.Int32Array(buffer); 
  8.     var HU32 = new global.Uint32Array(buffer); 
  9.     var log = foreign.consoleDotLog; 
  10.  
  11.     // ------------------------------------------------------------------------- 
  12.     // SECTION 2: functions 
  13.  
  14.     function f(x, y, z, w) { 
  15.         // SECTION A: parameter type declarations 
  16.         x = x|0;      // int parameter 
  17.         y = +y;       // double parameter 
  18.  
  19.         // SECTION B: function body 
  20.         log(x|0);     // call into FFI -- must force the sign 
  21.         log(y);       // call into FFI -- already know it's a double 
  22.         x = (x+3)|0;  // signed addition 
  23.  
  24.         // SECTION C: unconditional return 
  25.         return ((((x+1)|0)>>>0)/(x|0))>>>0; // compound expression 
  26.     } 
  27.  
  28.     function g() { 
  29.         g_f = +g_i; // read/write globals 
  30.         return; 
  31.     } 
  32.  
  33.     function g2() { 
  34.         return; 
  35.     } 
  36.  
  37.     function h(i, x) { 
  38.         i = i|0; 
  39.         x = x|0; 
  40.         H32[(i&0xffffffff)>>4] = x; // masked by 2^k-1, shifted by byte count 
  41.         ftable_2[(x-2)&2]();        // dynamic call of functions in table 2 
  42.     } 
  43.  
  44.     // ------------------------------------------------------------------------- 
  45.     // SECTION 3: function tables 
  46.  
  47.     var ftable_1 = [f]; 
  48.     var ftable_2 = [g, g2]; // all of the same type 
  49.  
  50.     // ------------------------------------------------------------------------- 
  51.     // SECTION 4: globals 
  52.  
  53.     var g_i = 0;   // int global 
  54.     var g_f = 0.0; // double global 
  55.  
  56.     // ------------------------------------------------------------------------- 
  57.     // SECTION 5: exports 
  58.  
  59.     return { f_export: f, goop: g }; 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.