# Better, Faster, Code

**URL:** <https://forum.kirupa.com/t/better-faster-code/649131>\
**Category:** design\
**Created:** [October 3, 2021, 2:42am UTC](https://forum.kirupa.com/t/better-faster-code/649131 "2021-10-03T02:42:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Belfry777](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/belfry777/32/12917_2.png) [@Belfry777](https://forum.kirupa.com/u/Belfry777)\
**Post date:** [October 3, 2021, 2:42am UTC](https://forum.kirupa.com/t/better-faster-code/649131/1 "2021-10-03T02:42:37Z")

</div>

Hi,  
In the never-ending quest to make our code faster, more efficient, and simpler, I have a number of questions to ask you all (the experienced and sage veterans) what JavaScript code structures (objects v. arrays for instance) are more efficient than others. I would like some general tips and, if possible, articles that explain the subject more in depth.  
Thank you in advance,

---

<div class="post-metadata">

**Author:** ![steve.mills](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/steve.mills/32/14961_2.png) [@steve.mills](https://forum.kirupa.com/u/steve.mills)\
**Post date:** [October 3, 2021, 1:26pm UTC](https://forum.kirupa.com/t/better-faster-code/649131/2 "2021-10-03T13:26:00Z")

</div>

I don’t know why but functions that return objects in Javascript (iterators and generators) are way more performant than closures.  
There are no zero cost abstractions in JS.

If you have a nested for loop or recursive function consider replacing it with a generator and `*yield` another generator e.g.

```auto
const nestedFor = function*(obj){
for(let i of obj){
yield i + 1;
}
const myGen = function*(obj){
for let(i of obj){
if(typeof(i) == 'object') yield* nestedFor(i);
else yield i + 1;
}}

```

Functional type programming although great for code re-use isn’t that great for performance. `function => function => function => function =>`

On that note `async await` is more performant than `Promise.then()`, probably because I think under the hood it’s similar to `generators` (there were `async await` hack libraries using `generators`(ES6) before official `async await` in ES7) and it’s not returning a `new Promise` object every `.then()`.

Also you have to constantly test performance and re write code to do the minimal amount of work whilst getting the job done.

I just re wrote a streaming module to `async iterators` and a few `async generators` and reduced the performance time from 30ms to 2.5ms.

---

<div class="post-metadata">

**Author:** ![Belfry777](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/belfry777/32/12917_2.png) [@Belfry777](https://forum.kirupa.com/u/Belfry777)\
**Post date:** [October 5, 2021, 11:54pm UTC](https://forum.kirupa.com/t/better-faster-code/649131/3 "2021-10-05T23:54:57Z")

</div>

Thank you for the suggestions!  
I have not looked at JavaScript past ECMA6, so this is the first time I have heard the term “generator functions” or “async.” I also have been looking around and have found that there are a lot more features to JavaScript that I don’t know, so I will be learning them. Thank you again.
