How to write an efficient code for Function node?

Hello n8n community! This is a general question, and I have no problems – but I’m curious :wink:

I often do data transformations with the Function node. An example can be this code (this is a WIP so please don’t look at the variable naming :see_no_evil: :smile:):

const parameterArray = [];
const data = $items("data"); => {
  if (category.json.parameters.length > 0) {
      .map(p => parameterArray.push({
      json: p

const uniqueArray = parameterArray
  .filter(p => p.json.values.length > 0)
  .filter(function (item, pos, self) {
  return self.indexOf(item) == pos;

const valueArray = []; => => valueArray.push({
    json: {
      value: value,
      parameterId: parameter.json.parameterId

return valueArray;

I’m a javascript beginner, and I’d like to know if there’s an efficient way to write little functions like this one, especially in the context of n8n. Or if you have any comments related to this code, I’d be happy too.

Thanks for the inspiration!

1 Like

Hi @honzapav, as long as it works and you can read it it’s good code I’d say :smiley:

As for how to become more efficient I think the best way is to learn vanilla JS. This will work not only in n8n Function nodes but also everywhere else JS is used. I’ve shared some thoughts around this a while back in this thread: Count occurrences of each item in JSON - #18 by MutedJam

Hi @MutedJam – thank you! It makes sense! Set() is on my reading list (I discovered it as an alternative to the function in my code).
All the best!