question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

consider using a backwards while-loop for map

See original GitHub issue

map and other methods could be made a tiny bit faster when we would use a backwards loop, see http://stackoverflow.com/a/23998651/1246198 why it’s faster and so on.

...
**Look at this** 

    for( var index = 0 , length = array.length ; index < length ; index++ ) {
     
     //do stuff
    
    }

 1. Need to create at least 2 variables (index,length)
 2. Need to check if the index is smaller than the length
 3. Need to increase the index
 4. the `for` loop has 3 parameters

**Now tell me why this should be faster than:**

    var length = array.length;
    
    while( --length ) { //or length--
    
     //do stuff
    
    }

 1. One variable
 2. No checks
 3. the index is decreased (Machines prefer that)
 4. `while` has only one parameter
...

This is just a suggestion, doesn’t have to be this way in any way 😉

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
CrossEyecommented, Feb 1, 2017

Benchmark runs these as many times as it can. (It’s what underlies JSPerf, and a successor of sorts to JSLitmus.)

And I’m still not convinced:

const R1 = require("./ramda")
const R2 = require("./ramda2")
const Benchmark = require("benchmark")
const benchmarks = require("beautify-benchmark")

const tenK = R1.range(1, 10001);

const suite = new Benchmark.Suite

suite.add("normal ramda", () => {
  R1.map(n => n * n, tenK)
})
.add("ramda with reversed while", () => {
  R2.map(n => n * n, tenK)
})
.on("cycle", event => {
  benchmarks.add(event.target)
})
.on("complete", () => {
  benchmarks.log()
})
.run()
>node bench2

  2 tests completed.

  normal ramda              x 9,611 ops/sec ±0.92% (90 runs sampled)
  ramda with reversed while x 9,806 ops/sec ±1.41% (85 runs sampled)


>node bench2

  2 tests completed.

  normal ramda              x 9,937 ops/sec ±0.83% (90 runs sampled)
  ramda with reversed while x 9,771 ops/sec ±1.28% (88 runs sampled)


>node bench2

  2 tests completed.

  normal ramda              x 9,922 ops/sec ±0.81% (89 runs sampled)
  ramda with reversed while x 9,919 ops/sec ±0.86% (86 runs sampled)

Mostly I’m not convinced of the worth of chasing such micro-optimizations. If there is a really substantive difference, I would certainly be interested in talking about it. But I don’t see that here. I see years of history of engines one-upping each other in various parts, and performance gaining unevenly because of that, but no particular algorithmic advantage.

0reactions
mrtnbrodercommented, Feb 1, 2017

I’m definitely not convinced:

Now imagine a larger array and the function being run many times in just a few seconds (quite the real world scenario, with React + some render logic)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to iterate over a Set or Map in reverse order ...
There is no way to obtain a reversed iterator on Maps or Sets, as I have found out while trying to get the...
Read more >
How to iterate a map in reverse order – C++
In this article we will discuss how to iterate a map in reverse order. Map store the elements in sorted order of keys....
Read more >
Map() vs for() in Javscript for looping reversely
I want to loop over this array in a reverse manner and return a new array. So, my first guess was to use...
Read more >
How to traverse a STL map in reverse direction?
Now for traversing in reverse order we will iterate over the range b/w rbegin() & rend() using reverse_iterator. Reverse Iteration in map:
Read more >
Reverse the order of map in Apex or VF
You can't loop over maps directly, so you're either looping over the map's keySet (which is a Set<Object> , and similarly unordered), or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found