Code Node with Simple Object Definition Extremely Slow on First Execution (1500ms), Fast on Subsequent (7ms)

Describe the problem/error/question

I have a Code node in my workflow that simply defines a static JavaScript object and returns it.

const userData = {
    "name": "Hans-Christian",
    "firstName": "Hans",
    "lastName": "Rørby",
    "fullName": "Hans-Christian Rørby",
    "email": "[email protected]",
    "userRole": "account",
    "userPermissions": "admin",
    "whiteLabelUrl": "hans.com",
    "relationshipId": "1-234-567",
    "agencyName": "Hans' Door Knob Cleaner",
    "currentUrl": "https://hans.com/reports",
    "locationId": "sfsaf3feasfckascnmi930",
    "locationName": "Hans' Cleaner",
    "knowledgeRoot": "https://hans.com.knowledgebases.net",
    "platformVersion": "v2",
    "lc_enabled": "true",
    "accountId": "3r3ffsarfe33",
    "kbSpaceId": "f33afasdfasfa",
    "apiBaseUrl": "https://hans.apiUrl"
  }

return [userData];

When I run this workflow in production mode, this specific Code node consistently takes around 1500ms to execute, as shown in the execution logs.

When I test the workflow manually:
The first time I execute the node (or the workflow containing it after a period of inactivity), this Code node also takes approximately 1500ms.
On subsequent manual test executions (without waiting a long time), the same Code node executes very quickly, around 7ms, which is what I would expect.

This significant delay on the first run (both in production and during initial testing) for such a simple operation seems excessive. I understand there can be cold start overheads, but 1.5 seconds for defining an object feels like there might be something else at play or an opportunity for optimization within n8n’s Code node handling.

Is this expected behavior for a Code node on its first execution after a “cold” state? If so, what contributes to this long initialization time for a simple object definition? If not, what could be causing this?

Please share your workflow

Share the output returned by the last node

[
{
“name”: “Hans-Christian”,
“firstName”: “Hans”,
“lastName”: “Rørby”,
“fullName”: “Hans-Christian Rørby”,
“email”: “[email protected]”,
“userRole”: “account”,
“userPermissions”: “admin”,
“whiteLabelUrl”: “hans.com”,
“relationshipId”: “1-234-567”,
“agencyName”: “Hans’ Door Knob Cleaner”,
“currentUrl”: “https://hans.com/reports”,
“locationId”: “sfsaf3feasfckascnmi930”,
“locationName”: “Hans’ Cleaner”,
“knowledgeRoot”: “https://hans.com.knowledgebases.net”,
“platformVersion”: “v2”,
“lc_enabled”: “true”,
“accountId”: “3r3ffsarfe33”,
“kbSpaceId”: “f33afasdfasfa”,
“apiBaseUrl”: “https://hans.apiUrl
}
]

Information on your n8n setup

  • n8n version: 1.90.2
  • Database (default: SQLite): n8n Cloud managed, so the default I guess.
  • n8n EXECUTIONS_PROCESS setting (default: own, main): n8n Cloud managed, so the default I guess.
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Cloud
  • Operating system: We are on Cloud.

Hi Cris, I hope you’re doing well.

Sorry if I just wast you time, I know thats not the answer you’re looking for.
But whats the need for use a Code node for that instead of the native node “Edit Fields (Set)” ?

I mean, this looks like a detail of a bigger picture. But for the question can be use the correct node.

Cheers mate.

1 Like

Hey there,
I decided to use the code now as it was just an easy way of me setting the data as that is how I had it from another software.
I just changed it over to the set node and that made it finish in 1ms.
So thank you!

Cool man. I’m glady it worked.
Keep in mind that you can still manipulate / nest data on set nodes itself.
You can evaluate expressions or even write functions on the data field of the variables. It’s very handfull

Some examples:
user.id from one node
message.id from another node

session.userId = json.user.id
session.messageId = json.message.id

output
session
|_userId
|_sessionId

Mergin multiples objects into one.

or something like when you may have data on something like a message.media that may have data or be null from an webook.
You can sanizite it like

session.media = {{ $json.message.media || "undefined" }}

Or even some functions. Be creative.

Cheers!

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