Pre increment operator is slightly performing better than inline and post increment operator

    public void postIncrementCost(){
        LocalTime pre = LocalTime.now();
        long start = System.currentTimeMillis();
        System.out.println("start postIncremental @==> "+ pre);
        int x = Integer.MIN_VALUE;
        for(int i=0; i< Integer.MAX_VALUE; i++) {
            while (x < Integer.MAX_VALUE) {
                x++;
            }
            x = Integer.MIN_VALUE;
        }
        LocalTime post = LocalTime.now();
        System.out.println("end postIncremental @==&gt; "+ post);
        long end = System.currentTimeMillis();
        System.out.println("postIncremental consumed @==&gt; "+ (end - start));
    }

  
  public void preIncrementCost(){
        LocalTime pre = LocalTime.now();
        System.out.println("start preIncremental @==&gt; "+ pre);
        long start = System.currentTimeMillis();
        int x = Integer.MIN_VALUE;
        for(int i=0; i< Integer.MAX_VALUE; i++) {
            while (x < Integer.MAX_VALUE) {
                ++x;
            }
            x = Integer.MIN_VALUE;
        }
        LocalTime post = LocalTime.now();
        System.out.println("end preIncremental @==&gt; "+ post);
        long end = System.currentTimeMillis();
        System.out.println("preIncremental consumed @==&gt; "+ (end - start));
    }

 
   public void mathIncrementCost(){
        LocalTime pre = LocalTime.now();
        System.out.println("start mathIncremental @==&gt; "+ pre);
        long start = System.currentTimeMillis();
        int x = Integer.MIN_VALUE;
        for(int i=0; i< Integer.MAX_VALUE; i++) {
            while (x < Integer.MAX_VALUE) {
                x = x+1;
            }
            x = Integer.MIN_VALUE;
        }
        LocalTime post = LocalTime.now();
        System.out.println("end mathIncremental @==&gt; "+ post);
        long end = System.currentTimeMillis();
        System.out.println("mathIncremental consumed @==&gt; "+ (end - start));
    }

start postIncremental @==> 15:47:41.764
end postIncremental @==> 15:47:41.773
postIncremental consumed @==> 9
start preIncremental @==> 15:47:41.774
end preIncremental @==> 15:47:41.782
preIncremental consumed @==> 8
start mathIncremental @==> 15:47:41.782
end mathIncremental @==> 15:47:41.791
mathIncremental consumed @==> 9

Advertisements

react merge state

Using Object.assign() to create a copy of the object:

const items= Object.assign({}, this.state.items, item)
 this.setState({ items})


Using spread operator:

const items= { …this.state.items, item }
 this.setState({ items})

debounce-promise

the doc is really not clear.

https://www.npmjs.com/package/debounce-promise

https://npm.runkit.com/debounce-promise

while truely, it turns out the debounce action needs to be PRE defined and evaluated.

so only the function is pre-defined like this will work

const preDefined = debounce((num)=> console.log("whatever action here", num), 100);
   [1, 2, 3, 4].forEach(num => {
     preDefined(num);
     });

"whatever action here"
4

while calling debounce() directly, simply like direct method calls

  [1, 2, 3, 4].forEach(num => {
    debounce(console.log("always defining", num), 100);
    });

"always defining"
1
"always defining"
2
"always defining"
3
"always defining"
4

react render empty

according to react doc

Booleans, Null, and Undefined Are Ignored

falsenullundefined, and true are valid children. They simply don’t render. These JSX expressions will all render to the same thing:

https://reactjs.org/docs/jsx-in-depth.html#booleans-null-and-undefined-are-ignored

so



class x extends Component{
     render(){
       return(null);
    }
}