Intrinsic Objects
For memory-constrained environments, QuickJS allows you to create a minimal context and selectively add only the intrinsic objects you need. This can significantly reduce memory usage compared to creating a full context withJS_NewContext().
Overview
Instead of usingJS_NewContext() which includes all standard objects, you can:
- Create a raw context with
JS_NewContextRaw() - Add only the intrinsic objects you need
- Save memory by excluding unused functionality
JS_NewContextRaw
Creates a minimal JavaScript context without any intrinsic objects.The runtime in which to create the context
JSContext pointer, or NULL on failure.
Description
This creates a context with no intrinsic objects. You must add at least base objects withJS_AddIntrinsicBaseObjects() to have a functional JavaScript environment.
Intrinsic Functions
JS_AddIntrinsicBaseObjects
Adds essential base objects required for basic JavaScript functionality.The context to add base objects to
ObjectFunctionArrayStringNumberBooleanSymbolErrorand error typesMath- Basic prototypes and constructors
JS_NewContextRaw().
JS_AddIntrinsicDate
Adds theDate object and related functionality.
The context to add Date support to
DateconstructorDate.prototypemethods- Date parsing and formatting
JS_AddIntrinsicEval
Adds support for theeval() function.
The context to add eval support to
- Global
eval()function - Dynamic code evaluation capabilities
JS_AddIntrinsicRegExp
Adds support for regular expressions.The context to add RegExp support to
RegExpconstructorRegExp.prototypemethods- Regular expression literal syntax
- String methods that use RegExp (
match,replace, etc.)
JS_AddIntrinsicRegExpCompiler() being called first.
JS_AddIntrinsicRegExpCompiler
Adds the regular expression compiler.The context to add the RegExp compiler to
JS_AddIntrinsicRegExp() to enable regular expression compilation.
JS_AddIntrinsicJSON
Adds theJSON object with parse and stringify methods.
The context to add JSON support to
JSON.parse()JSON.stringify()
JS_AddIntrinsicProxy
Adds support for Proxy objects.The context to add Proxy support to
ProxyconstructorReflectobject- Proxy handler traps
JS_AddIntrinsicMapSet
Adds Map, Set, WeakMap, and WeakSet objects.The context to add Map/Set support to
MapandMap.prototypeSetandSet.prototypeWeakMapandWeakMap.prototypeWeakSetandWeakSet.prototype
JS_AddIntrinsicTypedArrays
Adds TypedArray and ArrayBuffer support.The context to add TypedArray support to
ArrayBufferSharedArrayBufferDataView- All typed array types:
Int8Array,Uint8Array,Uint8ClampedArrayInt16Array,Uint16ArrayInt32Array,Uint32ArrayFloat16Array,Float32Array,Float64ArrayBigInt64Array,BigUint64Array
JS_AddIntrinsicPromise
Adds Promise and async/await support.The context to add Promise support to
PromiseconstructorPromise.prototype.then(),catch(),finally()Promise.all(),race(),allSettled(),any()async/awaitsyntax support
JS_AddIntrinsicBigInt
Adds BigInt support for arbitrary-precision integers.The context to add BigInt support to
BigIntconstructorBigInt.prototypemethods- BigInt literal syntax (
123n) - BigInt arithmetic operations
JS_AddIntrinsicWeakRef
Adds WeakRef and FinalizationRegistry support.The context to add WeakRef support to
WeakRefconstructorFinalizationRegistryconstructor- Weak reference functionality
JS_AddPerformance
Adds the Performance API.The context to add Performance API to
performanceglobal objectperformance.now()for high-resolution timing
JS_AddIntrinsicDOMException
Adds the DOMException error type.The context to add DOMException to
DOMExceptionconstructor- DOM-style exception handling
Example: Minimal Context
Create a minimal context with only basic objects and JSON support:Example: Full-Featured Context
Create a context with all intrinsics (equivalent toJS_NewContext()):
Memory Savings
By selectively adding intrinsics, you can significantly reduce memory usage:- Minimal (BaseObjects only): ~50KB
- Basic (BaseObjects + JSON + Date): ~60KB
- Standard (BaseObjects + common features): ~80KB
- Full (All intrinsics): ~120KB
Best Practices
- Always start with base objects:
JS_AddIntrinsicBaseObjects()is required for basic functionality - Add RegExp compiler first: Call
JS_AddIntrinsicRegExpCompiler()beforeJS_AddIntrinsicRegExp() - Consider security: Omit
JS_AddIntrinsicEval()if you don’t need dynamic code execution - Profile your usage: Only add intrinsics that your code actually uses
- Document requirements: Clearly document which intrinsics your library or application needs