What is faster, string primitives (string conversion), or regular expression

Describe the problem/error/question On n8n, what has better performance, string primitives (string conversion), or regular expression with case insensitivity?

It appears string primitives are faster than regex in general, unless it’s compiled regex, which I’m not sure what n8n uses.

So in an IF node doing a REGEX MATCH, which would be faster

{{ $json.body.direction}}
{{ /^(up|down)$/i }}

{{ $json.body.direction.toLowerCase() }}
{{ /^(up|down)$/ }}

What is the error message (if any)? NONE

Please share your workflow

“meta”: {
“templateCredsSetupCompleted”: true,
“instanceId”: “5d81b031d071ea343947e29f1e3553f1aac61d04d0e5e9bd7aedadd24853ad2e”
“nodes”: [
“parameters”: {
“conditions”: {
“string”: [
“value1”: “={{ $json.body.direction}}”,
“operation”: “regex”,
“value2”: “={{ /^(up|down)$/i }}”
“id”: “cee85c65-60c8-410d-9fd6-ab42217256bc”,
“name”: “Input Validation - direction”,
“type”: “n8n-nodes-base.if”,
“typeVersion”: 1,
“position”: [
“connections”: {},
“pinData”: {}

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 1.25.1
  • Database (default: SQLite): n/a
  • n8n EXECUTIONS_PROCESS setting (default: own, main): main
  • Running n8n via (Docker, npm, n8n cloud, desktop app): n8n cloud
  • Operating system: n/a

Hey @ManOnFire1978,

I don’t think you would really notice the difference between either approach, Generally speaking though outside of n8n I believe regex is slightly slower but it all depends on what you are really doing and the data sizes.

I concur based on my internet searches?

Does n8n use compiled regex? I think n8n is using laravel? Is that performance-optimized or compiled?

Thanks @Jon

Hey @ManOnFire1978,

n8n is written using TypeScript on top of NodeJS if that helps.

Thanks it does.

So a Code Node with JS, is that executed directly at the NodeJS layer, or is there extra conversations/compilations that happen?

Hey @ManOnFire1978,

If you run a code node it will start a sandbox which will use more resources, You can find the code for the code node here: n8n/packages/nodes-base/nodes/Code at master · n8n-io/n8n · GitHub if you wanted to poke around it a bit more.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.