# Insert in nodejs is slow

**URL:** <https://discuss.elastic.co/t/insert-in-nodejs-is-slow/327705>\
**Category:** Elasticsearch\
**Tags:** docker\
**Created:** [March 14, 2023, 9:49pm UTC](https://discuss.elastic.co/t/insert-in-nodejs-is-slow/327705 "2023-03-14T21:49:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HF\_Mohammad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hf_mohammad/32/118457_2.png) [@HF\_Mohammad](https://discuss.elastic.co/u/HF_Mohammad)\
**Post date:** [March 14, 2023, 9:49pm UTC](https://discuss.elastic.co/t/insert-in-nodejs-is-slow/327705/1 "2023-03-14T21:49:15Z")

</div>

This is my code:

```auto
for (let i = 0; i < 500; i++) {
    await client.index({
      index: 'user',
      body: {
        name: 'mohammad',
        type: 'mobile'
      }
    })
  }

```

This code take time about 3 second to finish!  
I tested this code via mongodb and take only 1 second.  
I use docker in local for run elasticsearch

---

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [March 15, 2023, 2:03am UTC](https://discuss.elastic.co/t/insert-in-nodejs-is-slow/327705/2 "2023-03-15T02:03:59Z")

</div>

Hello,

It is not clear what is your issue here, can you provide any context?

From what you shared you are basically doing 500 requests to index the same document, this is not the correct way to evaluate indexing speed.

In a real use case you should the [bulk api](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/bulk_examples.html) if you want to index multiple documents.

---

<div class="post-metadata">

**Author:** ![HF\_Mohammad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hf_mohammad/32/118457_2.png) [@HF\_Mohammad](https://discuss.elastic.co/u/HF_Mohammad)\
**Post date:** [March 15, 2023, 7:23am UTC](https://discuss.elastic.co/t/insert-in-nodejs-is-slow/327705/3 "2023-03-15T07:23:08Z")

</div>

This is only for test.  
I have 10000 records.  
And I want to save this records to user table.  
And it's important for me use `await`, because next line I want to avoid duplication.

---

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [March 15, 2023, 12:48pm UTC](https://discuss.elastic.co/t/insert-in-nodejs-is-slow/327705/4 "2023-03-15T12:48:53Z")

</div>

> [@HF\_Mohammad](#):
>
> This is only for test.  
> I have 10000 records.

Yeah, but as a I said, this is not the correct way to test indexing speed. When indexing multiple documents you should use bulk to increase the indexing speed, if you do not use bulk you will have one API request for every document and this can be slow depending on the case/size of document.

> [@HF\_Mohammad](#):
>
> And it's important for me use `await`, because next line I want to avoid duplication.

If you want to avoid duplicate documents you need to use a custom `_id` value.

---

<div class="post-metadata">

**Author:** ![HF\_Mohammad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hf_mohammad/32/118457_2.png) [@HF\_Mohammad](https://discuss.elastic.co/u/HF_Mohammad)\
**Post date:** [March 15, 2023, 3:19pm UTC](https://discuss.elastic.co/t/insert-in-nodejs-is-slow/327705/5 "2023-03-15T15:19:45Z")

</div>

Thank you  
I have a realtime service and per second I receive more than 2000 message  
So I want to check if not exists, insert this record  
I can't use bulk, because maybe in this message have duplicated data  
My structure is:

get new message  
check if exists  
if exists =\> update it  
then =\> insert it

How is best solution in nodejs for it?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 12, 2023, 3:19pm UTC](https://discuss.elastic.co/t/insert-in-nodejs-is-slow/327705/6 "2023-04-12T15:19:59Z")

</div>

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