{"id":405,"date":"2018-01-22T14:34:04","date_gmt":"2018-01-22T18:34:04","guid":{"rendered":"https:\/\/timesheet.dovico.com\/blog\/?p=405"},"modified":"2025-08-13T17:37:50","modified_gmt":"2025-08-13T20:37:50","slug":"an-introduction-to-webassembly","status":"publish","type":"post","link":"https:\/\/timesheet.dovico.com\/blog\/2018\/01\/22\/an-introduction-to-webassembly\/","title":{"rendered":"An Introduction to WebAssembly"},"content":{"rendered":"\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"678\" src=\"https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2018\/01\/webdesign-project-desk-PKH79U9-1024x678.jpg\" alt=\"\" class=\"wp-image-415\" srcset=\"https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2018\/01\/webdesign-project-desk-PKH79U9-1024x678.jpg 1024w, https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2018\/01\/webdesign-project-desk-PKH79U9-300x199.jpg 300w, https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2018\/01\/webdesign-project-desk-PKH79U9-768x509.jpg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"graf graf--p graf-after--h3\">The concept behind WebAssembly isn\u2019t new and is based on work that was pioneered by Mozilla&nbsp;<em class=\"markup--em markup--p-em\">(asm.js)<\/em>&nbsp;and Google&nbsp;<em class=\"markup--em markup--p-em\">(Native Client \u2014 NaCl and Portable Native Client \u2014 PNaCl)<\/em>.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">One of WebAssembly\u2019s main goals is parity with asm.js so I\u2019ll give you a little background on that before we start digging into WebAssembly.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">When I first heard of asm.js a few years ago I was excited. The ability to write code in C\/C++ and have it compiled down into a special form of JavaScript that the browser could then run at near-native speeds if it supported asm.js.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\"><em>You don\u2019t typically write asm.js code by hand, it\u2019s usually created by a compiler.<\/em><\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">The code starts off with the&nbsp;<strong class=\"markup--strong markup--p-strong\">asm pragma<\/strong>&nbsp;statement (&#8220;use asm&#8221;;) and then typing hints are included that allow JavaScript interpreters, that support asm.js, to know that they can use low-level CPU operations rather than the more expensive JavaScript operations.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">For example,&nbsp;<strong class=\"markup--strong markup--p-strong\">a | 0<\/strong>&nbsp;is used to hint that the variable \u2018a\u2019 is a 32-bit integer. This works because a bitwise operation of zero doesn\u2019t change the original value so there are no side effects to doing this. Because there are no side effects to this, it can be used wherever it\u2019s required in the code to hint the type for either the return value or the parameters passed into a method as in the following example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function AsmModule() {\n  \"use asm\";       \n  return {\n    add: function(a, b) {\n      a = a | 0;           \n      b = b | 0;           \n      return (a + b) | 0;\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\">The nice thing about asm.js is that, even if the browser doesn\u2019t support it, it would still run and you would get identical results since the code is still valid JavaScript. The only difference is that it would be slower compared to if the browser did support asm.js.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">Being able to write code in C\/C++ and have it compiled into a special form of JavaScript that is significantly faster than standard JavaScript is pretty cool but asm.js did have some disadvantages:<\/p>\n\n\n\n<ul class=\"postList wp-block-list\"><li>The extra type hints could result in very large asm.js files<\/li><li>Things still need to be parsed so a large file could be expensive on lower-end devices like phones<\/li><li>asm.js needs to be valid JavaScript so adding new features is complex and would affect the JavaScript language itself as well<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">WebAssembly<\/h3>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">To solve the issues of asm.js, the major browser vendors got together and started working on a&nbsp;<a class=\"markup--anchor markup--p-anchor\" rel=\"nofollow noopener noreferrer\" data-href=\"https:\/\/www.w3.org\/community\/webassembly\/\" href=\"https:\/\/www.w3.org\/community\/webassembly\/\" target=\"_blank\">W3C standard<\/a>&nbsp;and an MVP of WebAssembly that is already live in most browsers! The browsers that currently support WebAssembly are:<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">Firefox, Chrome, Opera, Edge 16, Safari 11, iOS Safari 11, Android browsers 56, Chrome for Android, and Firefox for Android!<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">It can be turned on in Edge 15 by turning on the browser\u2019s Experimental JavaScript Features flag.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">WebAssembly, or wasm for short, is intended to be a portable bytecode that will be efficient for browsers to download and load. The bytecode is transmitted in a binary format and, due to how the modules are structured, things can be compiled by the browser in parallel speeding things up even further.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">Once the binary has been compiled into executable machine code, the module is stateless and as a result can be explicitly cached in IndexedDB or even shared between windows and workers via postMessage calls.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">WebAssembly is currently an MVP so not everything is there yet but it will eventually include both a binary notation, that compilers will produce, and a corresponding text notation for display in debuggers or development environments.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Emscripten<\/h3>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">For the examples that follow, I\u2019ll be using Emscripten to compile C code into a wasm module. You can download Emscripten from the following location:&nbsp;<a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/kripken.github.io\/emscripten-site\/docs\/getting_started\/downloads.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" data-href=\"https:\/\/kripken.github.io\/emscripten-site\/docs\/getting_started\/downloads.html\">https:\/\/kripken.github.io\/emscripten-site\/docs\/getting_started\/downloads.html<\/a><\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">On Windows it was simply a matter of unzipping the contents and then running some command line arguments.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">I was getting errors when I first tried running a wasm module and it turned out that the version of Emscripten that came with the zip files wasn\u2019t recent enough.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">You\u2019ll need git on your system in order to have the command line arguments build the latest version of the Emscripten compiler for you. The following command line downloaded git for me:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>emsdk install git-1.9.4<\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\">Once you have the latest version of git on your system, run the command lines indicated on the download page to update, install, and activate Emscripten.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">Note: When you run the \u2018activate latest\u2019 command line, you will probably want to include the&nbsp;<strong class=\"markup--strong markup--p-strong\">&#8211;global<\/strong>&nbsp;flag.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">Otherwise, the path variables are only remembered for the current command line window and you\u2019ll have to run the emsdk_env.bat file each time you open a command-line window:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>emsdk activate latest --global<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Hello World<\/h3>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">For the examples that follow, I simply create a text file named test.c and use notepad to adjust the text&nbsp;<em class=\"markup--em markup--p-em\">(you don\u2019t need an IDE for the examples I give you here)<\/em>.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">As with any new programming technology, it\u2019s almost a requirement to start off with a hello world program so let\u2019s create a very basic hello world app using C. The following will simply write a string to the command line as soon as the module gets loaded:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h>    \n\nint main()  \n{\n  printf(\"Hello World from C\\n\");     \n  return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\">To compile the code into a WebAssembly module, we need to run the following command line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>emcc test.c -s WASM=1 -o hello.html<\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\"><strong class=\"markup--strong markup--p-strong\">-s WASM=1<\/strong>&nbsp;specifies that we want a wasm module output<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">The nice thing about this tool is that it gives you all the JavaScript \u2018glue\u2019 needed allowing you to play with WebAssembly modules right away. As you get more experienced with the technology, you can customize it.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">If you open the generated hello.html file in a browser, you\u2019ll see the&nbsp;<strong class=\"markup--strong markup--p-strong\">Hello World from C<\/strong>&nbsp;text displayed in the textbox.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">For the rest of the examples, I\u2019m going to adjust the command line to generate a more minimal html template just to make things easier&nbsp;<em class=\"markup--em markup--p-em\">(less to scroll through when we edit the html file)<\/em>.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">I\u2019ve copied the&nbsp;<strong class=\"markup--strong markup--p-strong\">emscripten.h<\/strong>&nbsp;and&nbsp;<strong class=\"markup--strong markup--p-strong\">shell_minimal.html<\/strong>&nbsp;files into folders in the root directory where I\u2019m creating my code files. For each code example, I\u2019ve created a subfolder for the&nbsp;.c file I create and for the generated files just to keep each example separate which is why you\u2019ll see a relative path stepping back a folder in the following examples.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">You can find the shell_minimal.html file in the emscripten&nbsp;<strong class=\"markup--strong markup--p-strong\">src<\/strong>&nbsp;folder.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">You can find the emscripten.h file in the emscripten&nbsp;<strong class=\"markup--strong markup--p-strong\">system\\include\\emscripten<\/strong>&nbsp;folder.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Calling into a module from JavaScript<\/h3>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">Let\u2019s take our code a step further and build a function that you can call from JavaScript.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">First, let\u2019s add a function to our code called&nbsp;<strong class=\"markup--strong markup--p-strong\">TestFunction<\/strong>&nbsp;that accepts an integer:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h>\n#include \"..\/emscripten\/emscripten.h\"\n\nint main()   \n{\n  printf(\"Hello World from C\\n\");     \n  return 0;\n}\n\nvoid EMSCRIPTEN_KEEPALIVE TestFunction(int iVal) {\n  printf(\"TestFunction called...value passed in was: %i\\n\", iVal);\n}<\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\">The&nbsp;<strong class=\"markup--strong markup--p-strong\">EMSCRIPTEN_KEEPALIVE<\/strong>&nbsp;declaration adds our functions to the exported functions list so that they\u2019re seen by the JavaScript code.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">Because we have a function we want to call in the WebAssembly, we don\u2019t want the runtime to shut down after the main method finishes executing so we\u2019re also going to include the&nbsp;<strong class=\"markup--strong markup--p-strong\">NO_EXIT_RUNTIME<\/strong>&nbsp;flag.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">Run the following to generate the new files:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>emcc test.c -s WASM=1 -o hello2.html --shell-file ..\/html_template\/shell_minimal.html -s NO_EXIT_RUNTIME=1<\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\">If you opened the html file in a browser at this point, you won\u2019t see anything other than the Hello World output because the JavaScript doesn\u2019t yet have the code to call TestFunction.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">Open up the generated hello2.html file using a tool like notepad, scroll down to just before the opening Script tag, and add the following HTML:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;input type=\"text\" id=\"txtValue\" \/> \n&lt;input type=\"button\" value=\"Pass Value\" onclick=\"PassValue();\" \/><\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\">Scroll further down the html file and add the following just before the closing script tag&nbsp;<em class=\"markup--em markup--p-em\">(after the window.onerror method\u2019s code)<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function PassValue(){\n  Module.ccall('TestFunction', \/\/ name of C function\n    null, \/\/return type \n    ['number'],\/\/argument types \n    [ parseInt(document.getElementById(\"txtValue\").value,10) ]);\n}<\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\">Save the html file and then open it up in a browser.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">If you type a number into the textbox next to the Pass Value button and then press the button you should see that value echoed back into the textbox.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">In the example above, we used the&nbsp;<strong class=\"markup--strong markup--p-strong\">ccall<\/strong>&nbsp;method which calls the module right away.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">Another approach is that we can use the&nbsp;<strong class=\"markup--strong markup--p-strong\">cwrap<\/strong>&nbsp;method to create a function pointer that can then be used multiple times. The JavaScript would look like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var fncTestFunction = Module.cwrap('TestFunction', null, ['number']);   \nfncTestFunction(1); \/\/passing in an integer directly for this test   \nfncTestFunction(2); \/\/passing in an integer directly for this test<\/code><\/pre>\n\n\n\n<div class=\"wp-block-ugb-cta ugb-cta ugb-53b817a ugb-cta ugb-cta--v2 ugb-main-block\" id=\"\"><style>.ugb-53b817a .ugb-cta__item{background-color:#000000 !important;background-image:url(https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2020\/01\/images.jpg);background-position:top left}.ugb-53b817a .ugb-cta__item:before{background-color:#000000 !important;opacity:0.6}.ugb-53b817a .ugb-cta__title{font-size:28px !important;color:#ffffff}.ugb-53b817a .ugb-cta__description{color:#ffffff}<\/style><div class=\"ugb-inner-block\"><div class=\"ugb-block-content\"><div class=\"ugb-cta__item ugb--has-background-overlay\"><h5 class=\"ugb-cta__title\">Check out Gerard&#8217;s new book: <em>WebAssembly In Action<\/em><\/h5><p class=\"ugb-cta__description\"><em>WebAssembly in Action<\/em>&nbsp;introduces the WebAssembly stack and walks you through the process of writing and running browser-based applications. Expert developer Gerard Gallant gives you a firm foundation of the structure of a module, HTML basics, JavaScript Promises, and the WebAssembly JavaScript API. <\/p><div class=\"ugb-button-container\"><a class=\"ugb-button ugb-button--size-normal\" href=\"https:\/\/www.manning.com\/books\/webassembly-in-action\" target=\"_blank\" rel=\"noopener noreferrer\"><span class=\"ugb-button--inner\">Available Now!<\/span><\/a><\/div><\/div><\/div><\/div><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Calling into JavaScript from a module using macros<\/h3>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">Being able to talk to the WebAssembly from JavaScript is nice but what if you want to talk to JavaScript from the WebAssembly?<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">There are different ways that this can be achieved.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">The simplest way is through macros like&nbsp;<strong class=\"markup--strong markup--p-strong\">emscripten_run_script()<\/strong>&nbsp;or&nbsp;<strong class=\"markup--strong markup--p-strong\">EM_ASM()<\/strong>&nbsp;which basically trigger a JavaScript eval statement.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">Macros are not my recommended approach for production code especially if you\u2019re dealing with user supplied data but they could come in handy if you needed to do some quick debugging.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">Note: You need to use single quotes in the macros. Double quotes will cause a syntax error that is not detected by the compiler.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">To test out the EM_ASM macro, let\u2019s adjust TestFunction to simply call into JavaScript and display an alert:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h>   \n#include \"..\/emscripten\/emscripten.h\"    \n\nint main()   \n{\n  printf(\"Hello World from C\\n\");     \n  return 0;\n}    \n\nvoid EMSCRIPTEN_KEEPALIVE TestFunction(int iVal) {\n  printf(\"TestFunction called...value passed in was: %i\\n\", iVal);\n\n  EM_ASM(\n    alert('Test call from C to JS');\n    throw 'all done';\n  );\n}<\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\">Run the following to generate the new files:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>emcc test.c -s WASM=1 -o hello3.html --shell-file ..\/html_template\/shell_minimal.html -s NO_EXIT_RUNTIME=1<\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\">Open up the hello3.html file that was generated, scroll down to just before the opening Script tag, and add the following HTML:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;input type=\"text\" id=\"txtValue\" \/>   \n&lt;input type=\"button\" value=\"Pass Value\" onclick=\"PassValue();\" \/><\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\">Scroll further down the html file and add the following just before the closing script tag&nbsp;<em class=\"markup--em markup--p-em\">(after the window.onerror method\u2019s code)<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function PassValue(){\n  Module.ccall('TestFunction', \/\/ name of C function\n    null, \/\/return type\n    ['number'], \/\/argument types       \n    [ parseInt(document.getElementById(\"txtValue\").value,10) ]);\n}<\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\">Save the html file and then open it up in a browser. If you type a number into the textbox next to the Pass Value button and then press the button you will see that value echoed back into the textbox but you\u2019ll also see an alert displayed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Calling into JavaScript from a module using function pointers<\/h3>\n\n\n\n<p class=\"graf graf--p graf-after--p\">In the previous example, we used a macro to call into the JavaScript code but that\u2019s generally taboo, especially if you have user-supplied data since it uses eval in the background.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">The better approach, in my opinion, is to pass a function pointer to the WebAssembly module and have the C code call into that.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">In the JavaScript code, you can use&nbsp;<strong class=\"markup--strong markup--p-strong\">Runtime.addFunction<\/strong>&nbsp;to return an integer value that represents a function pointer. You can then pass that integer to the C code which can be used as a function pointer.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">When using Runtime.addFunction, there is a backing array where these functions are stored. The array size must be explicitly set, which can be done via a compile-time setting:&nbsp;<strong class=\"markup--strong markup--p-strong\">RESERVED_FUNCTION_POINTERS<\/strong>.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">Let\u2019s adjust our code by getting rid of TestFunction and adding in a new function called CallFunctionPointer that simply calls the function pointer that was specified:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h>   \n#include \"..\/emscripten\/emscripten.h\"    \n\nint main(){\n  printf(\"Hello World from C\\n\");     \n  return 0;\n}\n\nvoid EMSCRIPTEN_KEEPALIVE CallFunctionPointer(void(*f)(void)){\n  (*f)();\n}<\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\">Run the following to generate the new files and indicate that we will have 1 function pointer:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>emcc test.c -s WASM=1 -o hello4.html --shell-file ..\/html_template\/shell_minimal.html -s NO_EXIT_RUNTIME=1 -s RESERVED_FUNCTION_POINTERS=1<\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\">Open up the hello4.html file that was generated, scroll down to just before the opening Script tag, and add the following HTML:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;input type=\"button\" value=\"Test Pointer\" onclick=\"TestPointer();\" \/><\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\">Scroll further down the html file and add the following just before the closing script tag&nbsp;<em class=\"markup--em markup--p-em\">(after the window.onerror method\u2019s code)<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function TestPointer(){\n  \/\/ Create an anonymous function that will be called by the C code     \n  var pointer = Runtime.addFunction(function() { alert('I was called from C!'); });      \n\n  \/\/ Call the C code passing in the pointer reference     \n  Module.ccall('CallFunctionPointer', null, ['number'], [pointer]);      \n\n  \/\/ Remove the function pointer from the array     \n  Runtime.removeFunction(pointer);\n}<\/code><\/pre>\n\n\n\n<p class=\"graf graf--p graf-after--pre\">Save the html file and then open it up in a browser. If you press the Test Pointer button you will see an alert displayed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Current Limitations<\/h3>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">Like with JavaScript, WebAssembly is specified to be run in a safe, sandboxed execution environment which means it will enforce the browser\u2019s same-origin and permission policies too.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">WebAssembly is currently an MVP which means there are a number of things still missing. For example, at the moment there is no direct DOM access from the assembly which means you will have to call into the JavaScript code if you need to update the UI.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Future Improvements<\/h3>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">The browser makers are pushing ahead with this technology&nbsp;<em class=\"markup--em markup--p-em\">(for example, Google\u2019s Native Client has been deprecated in favor of this)<\/em>&nbsp;so I expect to see improvements start showing up.<\/p>\n\n\n\n<p class=\"graf graf--p graf-after--p\">The following are some of the improvements that browser makers have identified:<\/p>\n\n\n\n<ul class=\"postList wp-block-list\"><li>Faster function calls between JavaScript and WebAssembly<\/li><li>Faster load times<\/li><li>Working directly with the DOM<\/li><li>Exception handling<\/li><li>Better tooling support for things like debugging<\/li><li>Garbage collection which will make it easier for some languages like C# to target WebAssembly too<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2019\/11\/Gerard_Gallant_1.png\" alt=\"\" class=\"wp-image-2991\" width=\"154\" height=\"154\" srcset=\"https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2019\/11\/Gerard_Gallant_1.png 150w, https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2019\/11\/Gerard_Gallant_1-50x50.png 50w\" sizes=\"auto, (max-width: 154px) 100vw, 154px\" \/><\/figure>\n\n\n\n<p>C. Gerard Gallant <\/p>\n\n\n\n<p>Author of <a rel=\"noreferrer noopener\" href=\"https:\/\/www.manning.com\/books\/webassembly-in-action\" target=\"_blank\">WebAssembly in Action<\/a>, and<br>Dovico Senior Software Developer \/ Software Architect<\/p>\n\n\n\n<p><em>Want to learn more about Gerard?<\/em> <a rel=\"noreferrer noopener\" aria-label=\"Read his Dovico Employee Spotlight! (opens in a new tab)\" href=\"https:\/\/timesheet.dovico.com\/blog\/2019\/08\/23\/employee-spotlight-whos-gerard-gallant\/\" target=\"_blank\">Read his Dovico Employee Spotlight!<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-ugb-spacer ugb-spacer ugb-cb84db6 ugb-spacer--v2 ugb-main-block\"><div class=\"ugb-inner-block\"><div class=\"ugb-block-content\"><div class=\"ugb-spacer--inner\"><\/div><\/div><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-cyan-bluish-gray-color has-alpha-channel-opacity has-cyan-bluish-gray-background-color has-background\"\/>\n\n\n\n<div class=\"alignfull wp-block-ugb-cta ugb-cta ugb-1230f7a ugb-cta ugb-cta--v2 ugb-cta--design-plain ugb-main-block ugb--has-custom-content-width ugb-main-block--inner-full ugb--has-block-background ugb--has-background-overlay\"><style>.ugb-1230f7a .ugb-cta__title{font-family:\"Roboto\",Sans-serif !important;font-size:40px !important;letter-spacing:0.5px !important;color:#ffffff}.ugb-1230f7a .ugb-cta__description{margin-bottom:45px !important;font-family:\"Roboto\",Sans-serif !important;font-size:23px !important;letter-spacing:-0.1px !important;color:#ffffff}.ugb-1230f7a .ugb-button .ugb-button--inner{font-family:\"Roboto Condensed\",Sans-serif !important;font-weight:400 !important;letter-spacing:0.5px !important}.ugb-1230f7a .ugb-button{background-color:#ffdb98;border-radius:5px !important}.ugb-1230f7a .ugb-button .ugb-button--inner,.ugb-1230f7a .ugb-button svg:not(.ugb-custom-icon){color:#303d89 !important}.ugb-1230f7a .ugb-button:before{border-radius:5px !important}.ugb-1230f7a.ugb-cta{min-height:0px;background-color:#243de5;background-image:url(https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2020\/12\/dovico.png)}.ugb-1230f7a.ugb-cta > .ugb-inner-block{max-width:2500px !important}.ugb-1230f7a.ugb-cta:before{background-image:linear-gradient(180deg,#243de5 48%,#7bdcb5 100%);opacity:0.8}@media screen and (min-width:768px){.ugb-1230f7a.ugb-cta{margin-top:0px !important;margin-bottom:0px !important;padding-top:0px !important;padding-right:35px !important;padding-bottom:0px !important;padding-left:35px !important}}@media screen and (-ms-high-contrast:active),screen and (-ms-high-contrast:none){.ugb-1230f7a.ugb-cta{height:0px}}@media screen and (min-width:768px){.ugb-1230f7a > .ugb-inner-block > .ugb-block-content > *{padding-top:43px !important;padding-bottom:58px !important}}<\/style><div class=\"ugb-inner-block ugb-inner-block--full\"><div class=\"ugb-block-content\"><div class=\"ugb-cta__item\"><h5 class=\"ugb-cta__title\"><br><strong><span style=\"color: #ffffff;\" class=\"stk-highlight\">Is your project time tracking falling behind?<\/span><\/strong><\/h5><p class=\"ugb-cta__description\"> Ditch the clunky spreadsheets. Dovico Timesheet makes it easy to track time, stay on budget, and hit every deadline. Grow your business with confidence\u2014let\u2019s help you win.<\/p><div class=\"ugb-button-container\"><a class=\"ugb-button ugb-button--size-medium ugb--hover-effect-lift ugb--shadow-1\" href=\"https:\/\/trytimesheet.dovico.com\/\" target=\"_blank\" rel=\"noopener noreferrer nofollow\" title=\"\"><span class=\"ugb-button--inner\">Learn More<\/span><\/a><\/div><\/div><\/div><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-cyan-bluish-gray-color has-alpha-channel-opacity has-cyan-bluish-gray-background-color has-background is-style-default\"\/>\n","protected":false},"excerpt":{"rendered":"<p>The concept behind WebAssembly isn\u2019t new and is based on work that was pioneered by Mozilla&nbsp;(asm.js)&nbsp;and Google&nbsp;(Native Client \u2014 NaCl and Portable Native Client \u2014 PNaCl). One of WebAssembly\u2019s main goals is parity with asm.js so I\u2019ll give you a little background on that before we start digging into WebAssembly. When I first heard of asm.js a few years ago I was excited. The ability to write code in C\/C++ and have it compiled down&#8230;<\/p>\n","protected":false},"author":3,"featured_media":415,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-405","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"featured_image_urls_v2":{"full":["https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2018\/01\/webdesign-project-desk-PKH79U9.jpg",4000,2649,false],"thumbnail":["https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2018\/01\/webdesign-project-desk-PKH79U9-150x150.jpg",150,150,true],"medium":["https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2018\/01\/webdesign-project-desk-PKH79U9-300x199.jpg",300,199,true],"medium_large":["https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2018\/01\/webdesign-project-desk-PKH79U9-768x509.jpg",768,509,true],"large":["https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2018\/01\/webdesign-project-desk-PKH79U9-1024x678.jpg",1024,678,true],"1536x1536":["https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2018\/01\/webdesign-project-desk-PKH79U9.jpg",1536,1017,false],"2048x2048":["https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2018\/01\/webdesign-project-desk-PKH79U9.jpg",2048,1356,false],"thoughts-blog":["https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2018\/01\/webdesign-project-desk-PKH79U9-700x500.jpg",700,500,true],"thoughts-header":["https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2018\/01\/webdesign-project-desk-PKH79U9-1280x1024.jpg",1280,1024,true]},"post_excerpt_stackable_v2":"<p>The concept behind WebAssembly isn\u2019t new and is based on work that was pioneered by Mozilla&nbsp;(asm.js)&nbsp;and Google&nbsp;(Native Client \u2014 NaCl and Portable Native Client \u2014 PNaCl). One of WebAssembly\u2019s main goals is parity with asm.js so I\u2019ll give you a little background on that before we start digging into WebAssembly. When I first heard of asm.js a few years ago I was excited. The ability to write code in C\/C++ and have it compiled down into a special form of JavaScript that the browser could then run at near-native speeds if it supported asm.js. You don\u2019t typically write asm.js code&hellip;<\/p>\n","category_list_v2":"<a href=\"https:\/\/timesheet.dovico.com\/blog\/category\/uncategorized\/\" rel=\"category tag\">Uncategorized<\/a>","author_info_v2":{"name":"Dovico","url":"https:\/\/timesheet.dovico.com\/blog\/author\/ejohnston\/"},"comments_num_v2":"0 comments","yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>An Introduction to WebAssembly - Dovico Blog<\/title>\n<meta name=\"description\" content=\"Discover how WebAssembly builds on the foundations of asm.js to deliver near-native performance in the browser.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/timesheet.dovico.com\/blog\/2018\/01\/22\/an-introduction-to-webassembly\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"An Introduction to WebAssembly - Dovico Blog\" \/>\n<meta property=\"og:description\" content=\"Discover how WebAssembly builds on the foundations of asm.js to deliver near-native performance in the browser.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/timesheet.dovico.com\/blog\/2018\/01\/22\/an-introduction-to-webassembly\/\" \/>\n<meta property=\"og:site_name\" content=\"Dovico Blog\" \/>\n<meta property=\"article:published_time\" content=\"2018-01-22T18:34:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-13T20:37:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2018\/01\/webdesign-project-desk-PKH79U9-1024x678.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"678\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Dovico\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dovico\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"27 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/timesheet.dovico.com\/blog\/2018\/01\/22\/an-introduction-to-webassembly\/\",\"url\":\"https:\/\/timesheet.dovico.com\/blog\/2018\/01\/22\/an-introduction-to-webassembly\/\",\"name\":\"An Introduction to WebAssembly - Dovico Blog\",\"isPartOf\":{\"@id\":\"https:\/\/timesheet.dovico.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/timesheet.dovico.com\/blog\/2018\/01\/22\/an-introduction-to-webassembly\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/timesheet.dovico.com\/blog\/2018\/01\/22\/an-introduction-to-webassembly\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2018\/01\/webdesign-project-desk-PKH79U9.jpg\",\"datePublished\":\"2018-01-22T18:34:04+00:00\",\"dateModified\":\"2025-08-13T20:37:50+00:00\",\"author\":{\"@id\":\"https:\/\/timesheet.dovico.com\/blog\/#\/schema\/person\/f3b591bcd0171f30ebf2582d9be2551d\"},\"description\":\"Discover how WebAssembly builds on the foundations of asm.js to deliver near-native performance in the browser.\",\"breadcrumb\":{\"@id\":\"https:\/\/timesheet.dovico.com\/blog\/2018\/01\/22\/an-introduction-to-webassembly\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/timesheet.dovico.com\/blog\/2018\/01\/22\/an-introduction-to-webassembly\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/timesheet.dovico.com\/blog\/2018\/01\/22\/an-introduction-to-webassembly\/#primaryimage\",\"url\":\"https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2018\/01\/webdesign-project-desk-PKH79U9.jpg\",\"contentUrl\":\"https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2018\/01\/webdesign-project-desk-PKH79U9.jpg\",\"width\":4000,\"height\":2649},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/timesheet.dovico.com\/blog\/2018\/01\/22\/an-introduction-to-webassembly\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/timesheet.dovico.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"An Introduction to WebAssembly\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/timesheet.dovico.com\/blog\/#website\",\"url\":\"https:\/\/timesheet.dovico.com\/blog\/\",\"name\":\"Dovico Blog\",\"description\":\"Give Time Meaning.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/timesheet.dovico.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/timesheet.dovico.com\/blog\/#\/schema\/person\/f3b591bcd0171f30ebf2582d9be2551d\",\"name\":\"Dovico\",\"description\":\"Helping teams track time and manage projects since 1993. Dovico\u2019s timesheet software makes time tracking simple, insightful, and scalable. Learn more at dovico.com.\",\"sameAs\":[\"https:\/\/www.dovico.com\"],\"url\":\"https:\/\/timesheet.dovico.com\/blog\/author\/ejohnston\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"An Introduction to WebAssembly - Dovico Blog","description":"Discover how WebAssembly builds on the foundations of asm.js to deliver near-native performance in the browser.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/timesheet.dovico.com\/blog\/2018\/01\/22\/an-introduction-to-webassembly\/","og_locale":"en_US","og_type":"article","og_title":"An Introduction to WebAssembly - Dovico Blog","og_description":"Discover how WebAssembly builds on the foundations of asm.js to deliver near-native performance in the browser.","og_url":"https:\/\/timesheet.dovico.com\/blog\/2018\/01\/22\/an-introduction-to-webassembly\/","og_site_name":"Dovico Blog","article_published_time":"2018-01-22T18:34:04+00:00","article_modified_time":"2025-08-13T20:37:50+00:00","og_image":[{"width":1024,"height":678,"url":"https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2018\/01\/webdesign-project-desk-PKH79U9-1024x678.jpg","type":"image\/jpeg"}],"author":"Dovico","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Dovico","Est. reading time":"27 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/timesheet.dovico.com\/blog\/2018\/01\/22\/an-introduction-to-webassembly\/","url":"https:\/\/timesheet.dovico.com\/blog\/2018\/01\/22\/an-introduction-to-webassembly\/","name":"An Introduction to WebAssembly - Dovico Blog","isPartOf":{"@id":"https:\/\/timesheet.dovico.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/timesheet.dovico.com\/blog\/2018\/01\/22\/an-introduction-to-webassembly\/#primaryimage"},"image":{"@id":"https:\/\/timesheet.dovico.com\/blog\/2018\/01\/22\/an-introduction-to-webassembly\/#primaryimage"},"thumbnailUrl":"https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2018\/01\/webdesign-project-desk-PKH79U9.jpg","datePublished":"2018-01-22T18:34:04+00:00","dateModified":"2025-08-13T20:37:50+00:00","author":{"@id":"https:\/\/timesheet.dovico.com\/blog\/#\/schema\/person\/f3b591bcd0171f30ebf2582d9be2551d"},"description":"Discover how WebAssembly builds on the foundations of asm.js to deliver near-native performance in the browser.","breadcrumb":{"@id":"https:\/\/timesheet.dovico.com\/blog\/2018\/01\/22\/an-introduction-to-webassembly\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/timesheet.dovico.com\/blog\/2018\/01\/22\/an-introduction-to-webassembly\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/timesheet.dovico.com\/blog\/2018\/01\/22\/an-introduction-to-webassembly\/#primaryimage","url":"https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2018\/01\/webdesign-project-desk-PKH79U9.jpg","contentUrl":"https:\/\/timesheet.dovico.com\/blog\/wp-content\/uploads\/2018\/01\/webdesign-project-desk-PKH79U9.jpg","width":4000,"height":2649},{"@type":"BreadcrumbList","@id":"https:\/\/timesheet.dovico.com\/blog\/2018\/01\/22\/an-introduction-to-webassembly\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/timesheet.dovico.com\/blog\/"},{"@type":"ListItem","position":2,"name":"An Introduction to WebAssembly"}]},{"@type":"WebSite","@id":"https:\/\/timesheet.dovico.com\/blog\/#website","url":"https:\/\/timesheet.dovico.com\/blog\/","name":"Dovico Blog","description":"Give Time Meaning.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/timesheet.dovico.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/timesheet.dovico.com\/blog\/#\/schema\/person\/f3b591bcd0171f30ebf2582d9be2551d","name":"Dovico","description":"Helping teams track time and manage projects since 1993. Dovico\u2019s timesheet software makes time tracking simple, insightful, and scalable. Learn more at dovico.com.","sameAs":["https:\/\/www.dovico.com"],"url":"https:\/\/timesheet.dovico.com\/blog\/author\/ejohnston\/"}]}},"_links":{"self":[{"href":"https:\/\/timesheet.dovico.com\/blog\/wp-json\/wp\/v2\/posts\/405","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/timesheet.dovico.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/timesheet.dovico.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/timesheet.dovico.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/timesheet.dovico.com\/blog\/wp-json\/wp\/v2\/comments?post=405"}],"version-history":[{"count":12,"href":"https:\/\/timesheet.dovico.com\/blog\/wp-json\/wp\/v2\/posts\/405\/revisions"}],"predecessor-version":[{"id":3411,"href":"https:\/\/timesheet.dovico.com\/blog\/wp-json\/wp\/v2\/posts\/405\/revisions\/3411"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/timesheet.dovico.com\/blog\/wp-json\/wp\/v2\/media\/415"}],"wp:attachment":[{"href":"https:\/\/timesheet.dovico.com\/blog\/wp-json\/wp\/v2\/media?parent=405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/timesheet.dovico.com\/blog\/wp-json\/wp\/v2\/categories?post=405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/timesheet.dovico.com\/blog\/wp-json\/wp\/v2\/tags?post=405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}