> For the complete documentation index, see [llms.txt](https://docs.n8n.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.n8n.io/deploy/host-n8n/configure-n8n/expression-engines.md). Agents can also search and read this documentation through its [MCP server](https://docs.n8n.io/~gitbook/mcp).

# Expression engines

How a self-hosted n8n instance evaluates expressions, what the sandbox limits are, how the sandboxed engines differ from the legacy engine, and how to move an instance from legacy to vm.

The expression engine is the part of n8n that evaluates [expressions](https://docs.n8n.io/build/work-with-data/expressions-versus-data-nodes.md). On a self-hosted instance, you choose which engine runs them and how it's limited.

Use this page to:

* [Choose an engine](#how-n8n-evaluates-expressions) and understand its [limits](#sandbox-limits-for-the-vm-and-quickjs-engines).
* [Move from the legacy engine to the vm engine](#move-from-the-legacy-engine-to-the-vm-engine).
* [Make expressions faster](#make-expressions-faster).
* [Monitor the expression engine](#monitor-the-expression-engine-with-prometheus-metrics).

For the list of settings, refer to [Expression engine environment variables](https://docs.n8n.io/deploy/host-n8n/configure-n8n/basic-configuration/use-environment-variables/expression-engine.md).

## How n8n evaluates expressions

n8n evaluates expressions in two places, and each has its own engine:

* **Backend:** the n8n server evaluates expressions when a workflow runs. `N8N_EXPRESSION_ENGINE` selects this engine.
* **Editor:** your browser evaluates expressions while you build a workflow, to preview their results. `N8N_EXPRESSION_ENGINE_FRONTEND` selects this engine.

The two settings are independent. A change to the editor's engine doesn't change how workflows run.

The backend has three engines:

| Engine    | How it runs expressions                                                                                      | Status                                                                    |
| --------- | ------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------- |
| `vm`      | In an isolated V8 environment inside the n8n process, with its own memory, a time limit, and a memory limit. | Default from n8n 2.35.0. Use this engine.                                 |
| `quickjs` | In a QuickJS sandbox compiled to WebAssembly, with a time limit and a memory limit.                          | n8n doesn't support `quickjs` on the backend. Don't use it in production. |
| `legacy`  | In the same JavaScript environment as n8n, without isolation.                                                | Deprecated. n8n plans to remove it in a future version.                   |

The editor has two engines, `legacy` (the default) and `quickjs`. n8n recommends `quickjs`. Refer to [Use the quickjs engine in the editor](#use-the-quickjs-engine-in-the-editor).

{% hint style="warning" %}
**Avoid the `legacy` expression engine**

The `legacy` engine runs expressions without isolation, so it gives less protection against malicious expressions than the sandboxed engines. When the backend uses `legacy`, n8n logs a deprecation warning at startup. While you use it, only give permission to create or edit workflows to users you fully trust.
{% endhint %}

## Sandbox limits for the vm and quickjs engines

The `vm` and `quickjs` engines limit each expression. The `legacy` engine has no limits.

| Limit                   | Default | Setting                              | Error you see                      |
| ----------------------- | ------- | ------------------------------------ | ---------------------------------- |
| Time for one expression | 5000 ms | `N8N_EXPRESSION_ENGINE_TIMEOUT`      | `Expression timed out`             |
| Memory for one sandbox  | 128 MiB | `N8N_EXPRESSION_ENGINE_MEMORY_LIMIT` | `Expression exceeded memory limit` |

On the `vm` engine, n8n can't reuse a sandbox that exceeds the memory limit. Later expressions in the same execution that need the engine also fail. The next execution gets a new sandbox.

The editor's `quickjs` engine always uses a 5000 ms time limit and a 128 MiB memory limit. The backend settings don't change them.

## How the vm and quickjs engines differ from the legacy engine

The sandboxed engines work on a copy of your workflow data. The `legacy` engine works on the data itself. Most expressions give the same result on every engine. These are the differences you may see after you move from `legacy`:

| What the expression does                                                                                         | On `legacy`                                                      | On `vm` and `quickjs`                                                                                               | What to do                                                                                                                                                      |
| ---------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Writes to workflow data, such as `{{ $json.count++ }}`, `{{ $json.list.push(1) }}`, or `{{ $json.list.sort() }}` | The change stays in the node data, and later expressions see it. | The expression returns the same value, but the change applies to a copy that n8n discards when the expression ends. | Don't use expressions to store state. Use an **Edit Fields** node, or `$getWorkflowStaticData('global')` in a JavaScript **Code** node.                         |
| Calls a method of a class instance in node output, such as `{{ $json._id.toString() }}` on a MongoDB `ObjectId`  | Calls the method.                                                | The instance arrives as a plain object without its methods. `toString()` gives `[object Object]`.                   | Node output must contain JSON values. Use a node version that returns them, for example **MongoDB** node version 1.4. In a **Code** node, return plain objects. |
| Reads the output of a **Function**, **Function Item**, or **LangChain Code** node                                | Returns the value.                                               | Can return `undefined`.                                                                                             | Replace these deprecated nodes with the **Code** node.                                                                                                          |
| Reads a field that holds a function or a symbol                                                                  | Returns the value.                                               | Returns `undefined`.                                                                                                | Return JSON values from the node.                                                                                                                               |
| Reads a field that holds a Promise, a `WeakMap`, or a similar object                                             | Returns the object.                                              | Returns an empty object.                                                                                            | Return JSON values from the node.                                                                                                                               |
| Reads an array with empty slots from workflow data                                                               | Skips the empty slots.                                           | Treats each empty slot as `undefined`.                                                                              | Remove empty slots before the expression, for example with `filter()` in a **Code** node.                                                                       |
| Runs for a long time or builds a large value                                                                     | No limit.                                                        | Fails at the time or memory limit.                                                                                  | Refer to [Sandbox limits for the vm and quickjs engines](#sandbox-limits-for-the-vm-and-quickjs-engines).                                                       |
| Contains a chain of thousands of operators                                                                       | Returns `null`.                                                  | Fails with an error.                                                                                                | Split the expression.                                                                                                                                           |

Class instances usually only exist while the data is in memory. After n8n saves and reloads execution data, the data is plain JSON and gives the same result on every engine. This happens in queue mode, for example, or when you open a past execution.

`extractUrlPath()` can give a different result across engines for host names with non-ASCII characters and for some `file:` URLs. Refer to [String](https://docs.n8n.io/build/work-with-data/transform-data/expression-reference/string.md).

### Examples of expressions to change

A counter that relies on a write to workflow data works on `legacy` only:

```
// Works on legacy only: the write is lost on vm and quickjs
{{ $json.counter++ }}

// Works on every engine: compute the new value, don't store it
{{ $json.counter + 1 }}
```

A method call on a class instance returns a different value in the sandbox:

```
// On legacy: "65f1c0ffee0123456789abcd". On vm and quickjs: "[object Object]"
{{ $json._id.toString() }}

// Works on every engine once the node returns the ID as text
{{ $json._id }}
```

## Move from the legacy engine to the vm engine

If your instance sets `N8N_EXPRESSION_ENGINE=legacy`, n8n logs a deprecation warning at startup. Follow these steps to move to `vm`.

1. **Update n8n.** Use the latest stable version. Recent releases made the `vm` engine faster, reduced its memory use, and reduced its differences from `legacy`.
2. **Check your workflows** for the patterns in [How the vm and quickjs engines differ from the legacy engine](#how-the-vm-and-quickjs-engines-differ-from-the-legacy-engine) and [Expressions that stay slow in the sandbox](#expressions-that-stay-slow-in-the-sandbox).
3. **Switch a test instance first.** Remove `N8N_EXPRESSION_ENGINE`, or set it to `vm`. If the instance is busy, also apply the [recommended settings](#recommended-settings). Restart n8n.
4. **Run your busiest workflows** with realistic data. Compare the results and the execution times with the `legacy` instance.
5. **Watch the metrics** in [Monitor the expression engine with Prometheus metrics](#monitor-the-expression-engine-with-prometheus-metrics), together with CPU, memory, and event loop lag.
6. **Switch production** when the test instance behaves as expected. In queue mode, apply the same settings to main, worker, and webhook processes.

```bash
# Before: the legacy engine
export N8N_EXPRESSION_ENGINE=legacy

# After: the vm engine, with the optional settings for a busy instance
export N8N_EXPRESSION_ENGINE=vm
export N8N_EXPRESSION_ENGINE_NATIVE_EVALUATION=true
export N8N_EXPRESSION_ENGINE_LAZY_ACQUIRE=true
export N8N_EXPRESSION_ENGINE_COMPILE_CACHE=true
```

### Go back to the legacy engine

If you hit a problem that you can't solve on `vm`, go back to the previous behavior:

1. Set `N8N_EXPRESSION_ENGINE=legacy`.
2. If you turned on native evaluation, set `N8N_EXPRESSION_ENGINE_NATIVE_EVALUATION=false`. Native evaluation applies to every engine, so it stays on with `legacy` until you turn it off.
3. Restart every n8n process: main, worker, and webhook processes.

No data changes. If the first setting alone solves your problem, you can turn native evaluation on again.

The `legacy` engine runs expressions without isolation. Report the problem to n8n so that you can return to `vm`.

## Make expressions faster

A sandbox costs time for each expression, and time and memory for each sandbox that n8n creates. The `legacy` engine has neither cost, which is why some instances stayed on it. The settings below and changes to your workflows can reduce this overhead.

### Recommended settings

The defaults suit most instances. If your instance handles many items, webhook requests, or executions:

1. Update to the latest stable n8n version. Recent versions made the `vm` engine faster and reduced the memory of each sandbox.
2. Set `N8N_EXPRESSION_ENGINE_NATIVE_EVALUATION=true`. n8n then evaluates simple expressions without a sandbox. Available from n8n 2.43.0.
3. Set `N8N_EXPRESSION_ENGINE_LAZY_ACQUIRE=true`. n8n then creates a sandbox only when an expression needs the engine. Available from n8n 2.40.0.
4. Set `N8N_EXPRESSION_ENGINE_COMPILE_CACHE=true` to make sandbox creation faster on the `vm` engine. Available from n8n 2.40.0.

These settings are all off by default. n8n reads them at startup. In [queue mode](https://docs.n8n.io/deploy/host-n8n/configure-n8n/scaling/enable-queue-mode.md), set the same values on main, worker, and webhook processes.

Two settings that don't help throughput:

* A larger `N8N_EXPRESSION_ENGINE_POOL_SIZE`. The pool is a buffer for short bursts. Under sustained load, n8n creates sandboxes as needed.
* A larger `N8N_EXPRESSION_ENGINE_MEMORY_LIMIT`. Raise it only if an expression needs more than 128 MiB, and first check the [slow patterns](#expressions-that-stay-slow-in-the-sandbox).

### Evaluate simple expressions without a sandbox

Most expressions in a workflow are small. They read a field, compare two values, or tidy up some text:

```
{{ $json.email }}
{{ $json.status === 'paid' ? 'Done' : 'Open' }}
{{ $('Webhook').item.json.body.id }}
{{ $json.tags.filter(t => t.startsWith('a')).join(', ') }}
Hello {{ $json.firstName.trim() }}
```

A sandbox costs more than the work these expressions do. With native evaluation on, n8n computes them directly. n8n doesn't run the expression as JavaScript on this path. It reads the parsed expression and supports only a fixed set of operations. Every other expression runs in the engine that `N8N_EXPRESSION_ENGINE` selects, as before.

Simple here means a fixed list of reads, operators, and methods. Every expression outside that list goes to the engine, unchanged. The full list is in [When native evaluation helps](#when-native-evaluation-helps).

Native evaluation applies to every backend engine, including `legacy`. It has no time limit of its own: n8n sends work that's too large to the engine. With `vm` and `quickjs`, the sandbox limits then apply.

### When native evaluation helps

| Workflow condition                                                                   | What happens                                                                                                                                               | What to do                                                                                                               |
| ------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------ |
| A node receives many items, and its parameters hold simple expressions.              | n8n evaluates each parameter once for each item. Every one of those evaluations skips the sandbox.                                                         | Keep the expressions in per-item parameters simple.                                                                      |
| Many executions or webhook requests use only simple expressions.                     | By default, n8n usually takes a sandbox when an execution starts, before it knows whether an expression needs one. With lazy creation on, it creates none. | Turn on `N8N_EXPRESSION_ENGINE_LAZY_ACQUIRE` together with native evaluation.                                            |
| A parameter uses something outside the list below.                                   | The expression runs in the engine, at the same cost as before.                                                                                             | If the parameter runs for many items, compute the value in an earlier node and read the result with a simple expression. |
| The workflow contains a **Function**, **Function Item**, or **LangChain Code** node. | n8n doesn't use native evaluation anywhere in that workflow.                                                                                               | Replace these deprecated nodes with the **Code** node.                                                                   |
| Node output holds class instances, or large or deeply nested values.                 | n8n starts the expression natively, then hands it to the engine.                                                                                           | Return JSON values from the node, and keep the values that expressions read small.                                       |

<details>

<summary>What an expression can use and still qualify for native evaluation</summary>

An expression qualifies for native evaluation when it only uses the following:

* Reads of `$json`, `$parameter`, `$vars`, `$binary`, `$itemIndex`, and `$runIndex`, including nested fields and optional chaining (`?.`).
* Reads of other nodes: `$('Node name').item`, `.first()`, `.last()`, and `.all()`, the same on `$input`, and `$node['Node name'].json`.
* Text, number, boolean, and `null` values, and arrays of them.
* Comparison, arithmetic, and logical operators, and the conditional operator (`a ? b : c`).
* Text, number, and array methods that don't change the value they're called on, such as `toUpperCase()`, `trim()`, `includes()`, `replace()`, `slice()`, `toFixed()`, `join()`, and `concat()`.
* `some()`, `every()`, `find()`, `filter()`, and `map()` with a one-parameter arrow function, such as `$json.items.filter(i => i.active)`.
* Number-like fields of `$now` and `$today`, such as `$now.year` or `$now.toMillis()`.
* Most n8n [data transformation functions](https://docs.n8n.io/build/work-with-data/transform-data/expression-reference.md), except the ones for dates.

Anything else goes to the engine, unchanged. This includes function definitions, nested arrow functions, methods that change an array such as `sort()` or `push()`, date functions, `$jmespath()`, and reads of `$execution` or `$workflow`.

</details>

### Expressions that stay slow in the sandbox

Three patterns can stay slow on the `vm` and `quickjs` engines, each for a different reason.

**Going through all items in a parameter that runs once per item.** n8n evaluates most node parameters once for each input item. Take this parameter:

```
{{ $input.all().map(item => item.json.id) }}
```

It goes through every item, for every item: 1,000 items mean 1,000,000 item reads. This repeated work exists on every engine. The sandbox can add to it, because an expression that runs in the engine may also copy the items it reads into the sandbox. Read the current item with `$json` instead. If you need a value computed from all items, compute it once in an earlier node, such as **Aggregate**, **Summarize**, or **Code**, and read the result.

**Copying a list before you read it.** In the sandbox, a copy forces n8n to move the whole list across at once. The copy is usually unnecessary, because an expression can't change workflow data in the sandbox:

```
// Slow: copies the list, then reads it
{{ JSON.stringify($json.rows.slice()) }}
// Same result, without the copy
{{ JSON.stringify($json.rows) }}

// Slow: copies the list, then maps it
{{ [...$json.rows].map(r => r.id) }}
// Same result, without the copy
{{ $json.rows.map(r => r.id) }}
```

**Building one very large value in an expression.** The whole value must fit in the memory limit. Move heavy transformations to a **Code** node.

To find these patterns, search your workflows for `$input.all()`, `$items(`, and `.all()` in node parameters.

### A measured example

n8n measured one case where the cost of the engine shows most: a node with 33 parameters that each read one field, such as `{{ $json.name }}`, for 2,000 input items. The table shows the time for one evaluation, compared with `legacy`:

| Configuration                         | Time for one evaluation, `legacy` = 1 |
| ------------------------------------- | ------------------------------------- |
| `legacy`                              | 1                                     |
| `vm`                                  | 1.65                                  |
| Native evaluation on, with any engine | 0.31                                  |

Read this as one example, not as a rule for your instance:

* It comes from one machine and one workflow shape, where every expression qualifies for native evaluation.
* It measures expression evaluation only, not the work that nodes do.
* Expressions that do more work, or read large data, have a different ratio.

Among the n8n Cloud instances that n8n observed with native evaluation on, over seven days in November 2026, n8n evaluated about three in four expressions natively. The share differed between groups of instances, and it doesn't predict the share on yours.

After you update n8n or change a setting, measure your own workflows: compare execution duration, CPU, and event loop lag for the same workload before and after.

## Monitor the expression engine with Prometheus metrics

With `N8N_METRICS=true`, n8n exposes [Prometheus metrics](https://docs.n8n.io/deploy/host-n8n/keep-n8n-running/monitor-n8n.md) for expression evaluation. The names below use the default `n8n_` prefix.

n8n only emits these metrics when the backend uses the `vm` or `quickjs` engine and `N8N_EXPRESSION_ENGINE_OBSERVABILITY_ENABLED` keeps its default, `true`. With `legacy`, none of them exist, including the native evaluation counter.

| Metric                                                                        | Type      | What it tells you                                                                                                                                                                                                                                                      |
| ----------------------------------------------------------------------------- | --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `n8n_expression_evaluation_duration_seconds`                                  | Histogram | Duration of each engine evaluation, by `status` (`success` or `error`) and error `type`. Doesn't include native evaluations.                                                                                                                                           |
| `n8n_expression_native_evaluations_total`                                     | Counter   | Expressions that reached native evaluation, by `result`: `handled` (evaluated natively), `declined` (not a simple expression, sent to the engine), or `fallback` (the data needed the engine, with a `reason`). An expression that fails natively counts as `handled`. |
| `n8n_expression_pool_acquired_total`                                          | Counter   | Sandboxes taken from the pool. With lazy creation on, these are the sandboxes acquired when an expression needs the engine.                                                                                                                                            |
| `n8n_expression_pool_cold_start_sync_total`                                   | Counter   | Sandboxes that n8n had to create while a request waited, because the pool was empty.                                                                                                                                                                                   |
| `n8n_expression_pool_cold_start_sync_duration_seconds`                        | Histogram | How long those creations took.                                                                                                                                                                                                                                         |
| `n8n_expression_pool_replenish_failed_total`                                  | Counter   | Failed attempts to refill the pool.                                                                                                                                                                                                                                    |
| `n8n_expression_code_cache_hit_total`, `n8n_expression_code_cache_miss_total` | Counter   | Use of the compiled expression cache.                                                                                                                                                                                                                                  |

What to read from them:

* **The native share:** `handled` divided by the sum of `handled`, `declined`, and `fallback`.
* **A high `declined` count:** your workflows use many expressions that don't qualify. Start with the nodes that process the most items.
* **A high `fallback` count:** read its `reason` label. `too_large`, `too_deep`, and `too_much_work` point to large or deeply nested data. `unsupported_type`, `unsupported_method`, and `replace_pattern` point to data or calls that native evaluation doesn't support.
* **Slow engine evaluations:** the share of evaluations above a threshold, for example 50 ms.
* **Engine errors:** the error rate by `type`, such as `timeout` and `memory_limit`.

The engine's duration and error metrics only cover the expressions that reach the engine. When you turn on native evaluation, the engine keeps the costly expressions and loses the simple ones, so its average duration can go up even though the instance does less work.

To find individual slow or failing expressions, [trace expression evaluations with OpenTelemetry](https://docs.n8n.io/deploy/host-n8n/keep-n8n-running/trace-executions-with-opentelemetry/choose-which-spans-n8n-exports.md#trace-expression-evaluations). Expressions that n8n evaluates natively don't create a span.

## Use the quickjs engine in the editor

`N8N_EXPRESSION_ENGINE_FRONTEND=quickjs` makes the editor evaluate expression previews in a QuickJS sandbox, isolated from the rest of the page. It's available from n8n 2.41.0, and n8n recommends it. The default is `legacy`.

The sandbox needs WebAssembly. n8n's default content security policy allows it. If you set your own `N8N_CONTENT_SECURITY_POLICY`, its `script-src` directive must also allow WebAssembly. Without a `script-src` directive, the browser applies `default-src` to scripts, so the same rule applies to `default-src`.

* Add `'wasm-unsafe-eval'`, which n8n recommends because it allows nothing else.
* A policy that already contains `'unsafe-eval'` also allows WebAssembly.

With neither, the browser blocks the sandbox and the editor falls back to `legacy`. Because `legacy` itself needs `'unsafe-eval'`, expression previews then show `null`. Workflow executions aren't affected.

To check which engine the editor uses:

* **The configured engine:** call `GET /rest/settings` and read `data.expressionEngine`. This is the setting. It stays `quickjs` even when the sandbox failed to start.
* **The engine that runs:** open the browser console and reload the editor. The error `Failed to initialize the expression engine`, or a content security policy error about WebAssembly, means that the sandbox didn't start.

## Related resources

* [Configure n8n](https://docs.n8n.io/deploy/host-n8n/configure-n8n.md)
* [Expression engine environment variables](https://docs.n8n.io/deploy/host-n8n/configure-n8n/basic-configuration/use-environment-variables/expression-engine.md)
* [Security environment variables](https://docs.n8n.io/deploy/host-n8n/configure-n8n/basic-configuration/use-environment-variables/security.md)
* [Monitor n8n](https://docs.n8n.io/deploy/host-n8n/keep-n8n-running/monitor-n8n.md)
* [Fix memory issues](https://docs.n8n.io/deploy/host-n8n/configure-n8n/scaling/fix-memory-issues.md)
* [Measure performance](https://docs.n8n.io/deploy/host-n8n/configure-n8n/scaling/measure-performance.md)
