Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade For Teachers Spaces Get Certified Upgrade For Teachers Spaces
   ❮     
     ❯   

Node.js Tutorial

Node HOMENode IntroNode Get StartedNode JS RequirementsNode.js vs BrowserNode Cmd LineNode V8 EngineNode ArchitectureNode Event Loop

Asynchronous

Node AsyncNode PromisesNode Async/AwaitNode Errors Handling

Module Basics

Node ModulesNode ES ModulesNode NPMNode package.jsonNode NPM ScriptsNode Manage DepNode Publish Packages

Core Modules

HTTP ModuleHTTPS ModuleFile System (fs)Path ModuleOS ModuleURL ModuleEvents ModuleStream ModuleBuffer ModuleCrypto ModuleTimers ModuleDNS ModuleAssert ModuleUtil ModuleReadline Module

JS & TS Features

Node ES6+Node ProcessNode TypeScriptNode Adv. TypeScriptNode Lint & Formatting

Building Applications

Node FrameworksExpress.jsMiddleware ConceptREST API DesignAPI AuthenticationNode.js with Frontend

Database Integration

MySQL Get StartedMySQL Create DatabaseMySQL Create TableMySQL Insert IntoMySQL Select FromMySQL WhereMySQL Order ByMySQL DeleteMySQL Drop TableMySQL UpdateMySQL LimitMySQL Join
MongoDB Get StartedMongoDB Create DBMongoDB CollectionMongoDB InsertMongoDB FindMongoDB QueryMongoDB SortMongoDB DeleteMongoDB Drop CollectionMongoDB UpdateMongoDB LimitMongoDB Join

Advanced Communication

GraphQLSocket.IOWebSockets

Testing & Debugging

Node Adv. DebuggingNode Testing AppsNode Test FrameworksNode Test Runner

Node.js Deployment

Node Env VariablesNode Dev vs ProdNode CI/CDNode SecurityNode Deployment

Perfomance & Scaling

Node LoggingNode MonitoringNode PerformanceChild Process ModuleCluster ModuleWorker Threads

Node.js Advanced

MicroservicesNode WebAssemblyHTTP2 ModulePerf_hooks ModuleVM ModuleTLS/SSL ModuleNet ModuleZlib ModuleReal-World Examples

Hardware & IoT

RasPi Get StartedRasPi GPIO IntroductionRasPi Blinking LEDRasPi LED & PushbuttonRasPi Flowing LEDsRasPi WebSocketRasPi RGB LED WebSocketRasPi Components

Node.js Reference

Built-in ModulesEventEmitter (events)Worker (cluster)Cipher (crypto)Decipher (crypto)DiffieHellman (crypto)ECDH (crypto)Hash (crypto)Hmac (crypto)Sign (crypto)Verify (crypto)Socket (dgram, net, tls)ReadStream (fs, stream)WriteStream (fs, stream)Server (http, https, net, tls)Agent (http, https)Request (http)Response (http)Message (http)Interface (readline)

Resources & Tools

Node.js CompilerNode.js ServerNode.js QuizNode.js ExercisesNode.js SyllabusNode.js Study PlanNode.js Certificate

Node.jsWebAssembly


What is WebAssembly?

WebAssembly (Wasm) is a binary instruction format designed as a portable compilation target for high-level languages like C, C++, and Rust.

Key characteristics of WebAssembly include:

  • Binary format - Compact size that loads and executes faster than JavaScript
  • Near-native performance - Executes at speeds close to native machine code
  • Platform independent - Runs on browsers, Node.js, and other environments
  • Safety - Executes in a sandboxed environment with a strong security model

Unlike JavaScript, WebAssembly is a low-level binary format that isn't meant to be written by hand.

Instead, you compile code from other languages into WebAssembly.


WebAssembly Support in Node.js

Node.js provides built-in support for WebAssembly through the globalWebAssembly object (just like in browsers).

To check if your Node.js version supports WebAssembly:

Example: Check WebAssembly Support

console.log(typeof WebAssembly === 'object');
console.log(WebAssembly);

Note: WebAssembly support was first added in Node.js v8.0.0 and has improved in subsequent versions.



Using WebAssembly in Node.js

The WebAssembly API in Node.js provides several methods for working with WebAssembly modules:

MethodDescription
WebAssembly.compile()Compiles WebAssembly binary code into a WebAssembly module
WebAssembly.instantiate()Compiles and instantiates WebAssembly code
WebAssembly.validate()Validates a WebAssembly binary format
WebAssembly.ModuleRepresents a compiled WebAssembly module
WebAssembly.InstanceRepresents an instantiated WebAssembly module
WebAssembly.MemoryRepresents WebAssembly memory

Here's a basic example of loading and running a WebAssembly module:

Example: Running WebAssembly in Node.js

const fs = require('fs');

// Read the WebAssembly binary file
const wasmBuffer = fs.readFileSync('./simple.wasm');

// Compile and instantiate the module
WebAssembly.instantiate(wasmBuffer).then(result => {
  const instance = result.instance;
  
  // Call the exported 'add' function
  const sum = instance.exports.add(2, 3);
  console.log('2 + 3 =', sum); // Output: 2 + 3 = 5
});

Note: Thesimple.wasm file in this example would be a compiled WebAssembly module that exports anadd function.

You would typically create this by compiling C, C++, or Rust code.


Working with Different Languages

You can compile various languages to WebAssembly for use in Node.js:

C/C++ with Emscripten

Emscripten is a compiler toolchain for C/C++ that outputs WebAssembly.

Example C Code (add.c):

#include <emscripten.h>

EMSCRIPTEN_KEEPALIVE
int add(int a, int b) {
  return a + b;
}

Compile to WebAssembly:

emcc add.c -s WASM=1 -s EXPORTED_FUNCTIONS='["_add"]' -o add.js

Rust with wasm-pack

wasm-pack is a tool for building Rust-generated WebAssembly.

Example Rust Code (src/lib.rs):

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn add(a: i32, b: i32) -> i32 {
   a + b
}

Build with wasm-pack:

wasm-pack build --target nodejs

Advanced WebAssembly Usage

1. Working with Complex Data Structures

Passing complex data between JavaScript and WebAssembly requires careful memory management:

Example: Passing Arrays to WebAssembly

// JavaScript code
const wasmModule = await WebAssembly.instantiate(wasmBuffer, {
  env: {
    memory: new WebAssembly.Memory({ initial: 1 })
  }
});
// Allocate memory for an array of 10 integers (4 bytes each)
const arraySize = 10;
const ptr = wasmModule.exports.alloc(arraySize * 4);
const intArray = new Int32Array(wasmModule.exports.memory.buffer, ptr, arraySize);
// Fill array with values
for (let i = 0; i < arraySize; i++) {
  intArray[i] = i * 2;
}
// Call WebAssembly function to process the array
const sum = wasmModule.exports.processArray(ptr, arraySize);
console.log('Sum of array:', sum);
// Don't forget to free the memory
wasmModule.exports.dealloc(ptr, arraySize * 4);

Corresponding C Code (compiled to WebAssembly):

#include <stdlib.h>

int* alloc(int size) {
  return (int*)malloc(size);
}

void dealloc(int* ptr, int size) {
  free(ptr);
}

int processArray(int* array, int length) {
  int sum = 0;
  for (int i = 0; i < length; i++) {
    sum += array[i];
  }
  return sum;
}

2. Multithreading with WebAssembly

WebAssembly supports multithreading through Web Workers and SharedArrayBuffer:

Example: Parallel Processing with WebAssembly

// main.js
const workerCode = `
  const wasmModule = await WebAssembly.instantiate(wasmBuffer, {
    env: { memory: new WebAssembly.Memory({ initial: 1, shared: true }) }
  });

  self.onmessage = (e) => {
    const { data, start, end } = e.data;
    const result = wasmModule.exports.processChunk(data, start, end);
    self.postMessage({ result });
  };
`;

// Create worker pool
const workerCount = navigator.hardwareConcurrency || 4;
const workers = Array(workerCount).fill().map(() => {
  const blob = new Blob([workerCode], { type: 'application/javascript' });
  return new Worker(URL.createObjectURL(blob));
});

// Process data in parallel
async function processInParallel(data, chunkSize) {
  const results = [];
  let completed = 0;

  return new Promise((resolve) => {
    workers.forEach((worker, i) => {
      const start = i * chunkSize;
      const end = Math.min(start + chunkSize, data.length);

      worker.onmessage = (e) => {
        results[i] = e.data.result;
        completed++;

        if (completed === workerCount) {
          resolve(results);
        }
      };

      worker.postMessage({ data, start, end });
    });
  });
}

3. Debugging WebAssembly

Debugging WebAssembly can be challenging, but modern tools can help:

Using Source Maps with Emscripten

# Compile with debugging information and source maps
emcc -g4 --source-map-base http://localhost:8080/ -s WASM=1 -s EXPORTED_FUNCTIONS='["_main","_my_function"]' -o output.html source.c

Debugging in Chrome DevTools

  1. Open Chrome DevTools (F12)
  2. Go to the "Sources" tab
  3. Find your WebAssembly file in the file tree
  4. Set breakpoints and inspect variables as with JavaScript

Real-World WebAssembly Examples

1. Image Processing with WebAssembly

WebAssembly excels at CPU-intensive tasks like image processing:

// JavaScript wrapper for WebAssembly image processing
async function applyFilter(imageData, filterType) {
  const { instance } = await WebAssembly.instantiate(wasmBuffer, {
    env: { memory: new WebAssembly.Memory({ initial: 1 }) }
  });

  const { width, height, data } = imageData;

  // Allocate memory for image data
  const imageDataSize = width * height * 4; // RGBA
  const imageDataPtr = instance.exports.alloc(imageDataSize);

  // Copy image data to WebAssembly memory
  const wasmMemory = new Uint8Array(instance.exports.memory.buffer);
  wasmMemory.set(new Uint8Array(data.buffer), imageDataPtr);

  // Apply filter
  instance.exports.applyFilter(imageDataPtr, width, height, filterType);

  // Copy result back to ImageData
  const resultData = new Uint8ClampedArray(
    wasmMemory.slice(imageDataPtr, imageDataPtr + imageDataSize)
  );

  // Free allocated memory
  instance.exports.dealloc(imageDataPtr, imageDataSize);

  return new ImageData(resultData, width, height);
}

2. Cryptography

High-performance cryptographic operations with WebAssembly:

// Example: Using the Web Crypto API with WebAssembly
async function encryptData(data, keyMaterial) {
  // Import WebAssembly crypto module
  const { instance } = await WebAssembly.instantiateStreaming(
    fetch('crypto.wasm'),
    { env: { memory: new WebAssembly.Memory({ initial: 1 }) } }
  );

  // Generate IV (Initialization Vector)
  const iv = window.crypto.getRandomValues(new Uint8Array(12));

  // Prepare data
  const dataBytes = new TextEncoder().encode(JSON.stringify(data));
  const dataPtr = instance.exports.alloc(dataBytes.length);
  new Uint8Array(instance.exports.memory.buffer, dataPtr, dataBytes.length)
    .set(dataBytes);

  // Encrypt data using WebAssembly
  const encryptedDataPtr = instance.exports.encrypt(dataPtr, dataBytes.length);

  // Get encrypted data from WebAssembly memory
  const encryptedData = new Uint8Array(
    instance.exports.memory.buffer,
    encryptedDataPtr,
    dataBytes.length // In real usage, you'd track the actual encrypted size
  );

  // Clean up
  instance.exports.dealloc(dataPtr, dataBytes.length);

  return {
    iv: Array.from(iv),
    encryptedData: Array.from(encryptedData)
  };
}

Resources and Next Steps

WebAssembly in Node.js offers several advantages:

  • Performance - Near-native execution speed for computationally intensive tasks
  • Language choice - Use languages like C, C++, Rust, Go, and others in your Node.js apps
  • Code reuse - Reuse existing libraries and codebases from other languages
  • Isomorphic code - Share WebAssembly modules between browser and server

Common use cases include:

  • Image and video processing
  • Real-time audio processing
  • Cryptography and encryption
  • Scientific computing and simulations
  • Game development
  • Machine learning algorithms

Performance Comparison

To demonstrate the performance benefits, let's compare JavaScript and WebAssembly implementations of a recursive Fibonacci function:

JavaScript Implementation:

// Recursive Fibonacci in JavaScript (inefficient for demonstration)
function fibonacciJS(n) {
  if (n <= 1) return n;
  return fibonacciJS(n - 1) + fibonacciJS(n - 2);
}

C Implementation (to be compiled to WebAssembly):

#include <emscripten.h>

// WebAssembly-optimized Fibonacci function
EMSCRIPTEN_KEEPALIVE
int fibonacci_wasm(int n) {
  if (n <= 1) return n;
  
  int a = 0, b = 1, temp;
  for (int i = 2; i <= n; i++) {
    temp = a + b;
    a = b;
    b = temp;
  }
  
  return b;
}
    b = temp;
  }
  
  return b;
}

Performance Comparison Code:

const fs = require('fs');
const path = require('path');

// Read the WebAssembly binary file
const wasmBuffer = fs.readFileSync('./fibonacci.wasm');

// JavaScript implementation for comparison
function fibonacciJS(n) {
  if (n <= 1) return n;
  return fibonacciJS(n - 1) + fibonacciJS(n - 2);
}

// Compile and instantiate the WebAssembly module
WebAssembly.instantiate(wasmBuffer).then(result => {
  const { fibonacci_wasm } = result.instance.exports;
  
  // Test with a value that's computationally expensive
  const n = 40;
  
  // Measure WebAssembly performance
  const wasmStart = performance.now();
  const wasmResult = fibonacci_wasm(n);
  const wasmEnd = performance.now();
  
  // Measure JavaScript performance
  const jsStart = performance.now();
  const jsResult = fibonacciJS(n);
  const jsEnd = performance.now();
  
  console.log(`Fibonacci(${n})`);
  console.log(`WebAssembly: ${wasmResult} (${(wasmEnd - wasmStart).toFixed(2)} ms)`);
  console.log(`#"xrcise_webassembly.js" data-extensions="asp">



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2025 Movatter.jp