Convert Figma logo to code with AI

bellard logoquickjs

Public repository of the QuickJS Javascript Engine.

8,251
861
8,251
99

Top Related Projects

93,919

A modern runtime for JavaScript and TypeScript.

Ultra-lightweight JavaScript engine for the Internet of Things.

23,181

The official mirror of the V8 Git repository

106,466

Node.js JavaScript runtime ✨🐢🚀✨

1,422

Embedded JavaScript engine for C/C++

Quick Overview

QuickJS is a small and embeddable JavaScript engine. It supports the latest ECMAScript standard (ES2020) and can be used as a scripting language or as a library embedded in C programs.

Pros

  • Small Footprint: QuickJS has a small footprint, making it suitable for embedded systems and resource-constrained environments.
  • Fast Execution: QuickJS is designed for fast execution, with a focus on performance.
  • Modern JavaScript Support: QuickJS supports the latest ECMAScript standard, including features like async/await, modules, and more.
  • Embeddable: QuickJS can be easily embedded in C programs, allowing for seamless integration with other systems.

Cons

  • Limited Ecosystem: As a relatively new and niche JavaScript engine, QuickJS has a smaller ecosystem compared to more established options like Node.js or V8.
  • Lack of Widespread Adoption: QuickJS is not as widely adopted as other JavaScript engines, which may limit its use in larger projects or enterprise environments.
  • Fewer Third-Party Libraries: The limited ecosystem means that the availability of third-party libraries and tools for QuickJS may be more restricted.
  • Ongoing Development: As a relatively new project, QuickJS may still have some rough edges or areas that require further development and stabilization.

Code Examples

Here are a few code examples demonstrating the usage of QuickJS:

  1. Executing JavaScript Code:
#include <quickjs.h>

int main() {
    JSRuntime *rt = JS_NewRuntime();
    JSContext *ctx = JS_NewContext(rt);

    const char *code = "console.log('Hello, QuickJS!');";
    JSValue result = JS_Eval(ctx, code, strlen(code), "<input>", JS_EVAL_TYPE_GLOBAL);

    JS_FreeValue(ctx, result);
    JS_FreeContext(ctx);
    JS_FreeRuntime(rt);
    return 0;
}
  1. Calling a JavaScript Function:
#include <quickjs.h>

int main() {
    JSRuntime *rt = JS_NewRuntime();
    JSContext *ctx = JS_NewContext(rt);

    const char *code = "function add(a, b) { return a + b; }";
    JS_Eval(ctx, code, strlen(code), "<input>", JS_EVAL_TYPE_GLOBAL);

    JSValue func = JS_GetGlobalObject(ctx);
    func = JS_GetPropertyStr(ctx, func, "add");

    JSValue args[] = { JS_NewInt32(ctx, 2), JS_NewInt32(ctx, 3) };
    JSValue result = JS_CallFunctionValue(ctx, JS_UNDEFINED, func, 2, args);

    int sum;
    JS_ToInt32(ctx, &sum, result);
    printf("2 + 3 = %d\n", sum);

    JS_FreeValue(ctx, result);
    JS_FreeValue(ctx, func);
    JS_FreeContext(ctx);
    JS_FreeRuntime(rt);
    return 0;
}
  1. Handling Errors:
#include <quickjs.h>

int main() {
    JSRuntime *rt = JS_NewRuntime();
    JSContext *ctx = JS_NewContext(rt);

    const char *code = "throw new Error('Something went wrong');";
    JSValue result = JS_Eval(ctx, code, strlen(code), "<input>", JS_EVAL_TYPE_GLOBAL);

    if (JS_IsException(result)) {
        JSValue exception = JS_GetException(ctx);
        const char *error_message = JS_ToCString(ctx, exception);
        printf("Error: %s\n", error_message);
        JS_FreeCString(ctx, error_message);
        JS_FreeValue(ctx, exception);
    }

    JS_FreeValue(ctx, result);
    JS_FreeContext(ctx);
    JS_FreeRuntime(rt);
    return 0;

Competitor Comparisons

93,919

A modern runtime for JavaScript and TypeScript.

Pros of Deno

  • Built-in TypeScript support and modern JavaScript features
  • Secure by default with explicit permissions for file, network, and environment access
  • Comprehensive standard library and built-in tooling (formatter, linter, test runner)

Cons of Deno

  • Smaller ecosystem compared to Node.js, with fewer third-party packages
  • Potential compatibility issues with existing Node.js projects and dependencies
  • Steeper learning curve for developers familiar with Node.js

Code Comparison

QuickJS:

#include "quickjs.h"

JSContext *ctx = JS_NewContext(rt);
JSValue global_obj = JS_GetGlobalObject(ctx);
JS_SetPropertyStr(ctx, global_obj, "myValue", JS_NewInt32(ctx, 42));
JS_FreeValue(ctx, global_obj);

Deno:

const myValue = 42;
globalThis.myValue = myValue;
console.log(globalThis.myValue); // 42

Key Differences

  • QuickJS is a lightweight JavaScript engine written in C, while Deno is a secure runtime for JavaScript and TypeScript built on V8
  • Deno focuses on modern web standards and security, whereas QuickJS aims for small size and embeddability
  • QuickJS provides low-level C API for embedding, while Deno offers a high-level runtime environment with built-in tools and libraries

Ultra-lightweight JavaScript engine for the Internet of Things.

Pros of JerryScript

  • Designed for resource-constrained devices, making it suitable for IoT and embedded systems
  • Supports a wide range of architectures and platforms
  • Has a smaller memory footprint compared to QuickJS

Cons of JerryScript

  • Limited ECMAScript support (ES5.1 with some ES2015 features)
  • Slower execution speed for complex JavaScript code
  • Less active development and community support

Code Comparison

JerryScript:

jerry_init (JERRY_INIT_EMPTY);
jerry_value_t eval_ret = jerry_eval ((jerry_char_t *) "print('Hello, World!');", 24, JERRY_PARSE_NO_OPTS);
jerry_release_value (eval_ret);
jerry_cleanup ();

QuickJS:

JSRuntime *rt = JS_NewRuntime();
JSContext *ctx = JS_NewContext(rt);
JS_Eval(ctx, "console.log('Hello, World!');", 28, "<input>", JS_EVAL_TYPE_GLOBAL);
JS_FreeContext(ctx);
JS_FreeRuntime(rt);

Both examples demonstrate basic initialization, script evaluation, and cleanup. JerryScript uses a more C-style API, while QuickJS adopts a slightly higher-level approach. QuickJS provides a more familiar JavaScript environment with built-in console object, whereas JerryScript requires additional setup for similar functionality.

23,181

The official mirror of the V8 Git repository

Pros of v8

  • Higher performance and optimization for complex applications
  • Extensive ecosystem and tooling support
  • Robust JIT compilation for faster execution

Cons of v8

  • Larger codebase and memory footprint
  • More complex to embed in other projects
  • Steeper learning curve for contributors

Code Comparison

v8:

Local<Context> context = Context::New(isolate);
Context::Scope context_scope(context);
Local<String> source = String::NewFromUtf8(isolate, "'Hello' + ', World!'").ToLocalChecked();
Local<Script> script = Script::Compile(context, source).ToLocalChecked();
Local<Value> result = script->Run(context).ToLocalChecked();

QuickJS:

JSRuntime *rt = JS_NewRuntime();
JSContext *ctx = JS_NewContext(rt);
JSValue val = JS_Eval(ctx, "'Hello' + ', World!'", strlen("'Hello' + ', World!'"), "<input>", JS_EVAL_TYPE_GLOBAL);
const char *str = JS_ToCString(ctx, val);
JS_FreeValue(ctx, val);

Summary

v8 is a high-performance JavaScript engine with extensive optimization and tooling support, making it ideal for complex applications. However, it has a larger footprint and is more complex to embed. QuickJS, on the other hand, is lightweight and easy to embed, but may not offer the same level of performance for complex scenarios. The code comparison illustrates the difference in API complexity between the two engines.

106,466

Node.js JavaScript runtime ✨🐢🚀✨

Pros of Node.js

  • Extensive ecosystem with a vast library of npm packages
  • Well-established, mature platform with strong community support
  • Optimized for scalability and high-performance I/O operations

Cons of Node.js

  • Larger footprint and resource consumption
  • Slower startup time compared to QuickJS
  • More complex to embed in other applications

Code Comparison

Node.js:

const http = require('http');
const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello World\n');
});
server.listen(8080);

QuickJS:

import * as std from 'std';
import * as os from 'os';

std.print('Hello World\n');
os.exit(0);

Summary

Node.js is a full-featured JavaScript runtime designed for server-side applications, offering a rich ecosystem and excellent scalability. QuickJS, on the other hand, is a lightweight JavaScript engine focused on embedding and quick startup times. Node.js excels in large-scale applications and web servers, while QuickJS is better suited for embedded systems, command-line tools, and scenarios where minimal resource usage is crucial. The code comparison illustrates the difference in complexity between setting up a simple HTTP server in Node.js versus a basic script in QuickJS.

1,422

Embedded JavaScript engine for C/C++

Pros of v7

  • Smaller footprint and lower memory usage, suitable for embedded systems
  • Built-in support for networking and file I/O operations
  • More permissive license (MIT) compared to QuickJS's MIT/BSD-2-Clause

Cons of v7

  • Less complete ECMAScript compliance compared to QuickJS
  • Fewer language features and standard library functions
  • Less active development and community support

Code Comparison

v7:

v7_val_t v7_mk_function(struct v7 *v7, v7_cfunction_t *func) {
  return v7_mk_cfunction(v7, func);
}

QuickJS:

JSValue JS_NewCFunction(JSContext *ctx, JSCFunction *func, const char *name,
                        int length) {
  return JS_NewCFunction2(ctx, func, name, length, JS_CFUNC_generic, 0);
}

Both examples show function creation, but QuickJS offers more parameters and flexibility.

Summary

v7 is better suited for resource-constrained environments and offers built-in networking support. QuickJS provides more complete ECMAScript compliance and a richer feature set. The choice between them depends on the specific requirements of your project, balancing between resource efficiency and language feature completeness.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

The main documentation is in doc/quickjs.pdf or doc/quickjs.html.