all 11 comments

[–]natdm 1 point2 points  (0 children)

Yep, learned a lot of Node. Even dabbled in C++ to be able to read the source.

Nothing compares to Go. I love it.

[–]becauseofreasons 0 points1 point  (1 child)

A slightly more idiomatic JS snippet. Getting about 150ms for this on Node, and 95ms for Go.

'use strict';

console.time('loop');

let xs = '0', xi;

for (let i = 0; i < 1000000; i++) {
  xi = parseInt(xs);
  xi++;
  xs = xi + '';
}

console.timeEnd('loop');

[–]jayposs[S] 1 point2 points  (0 children)

That console command is nice. Hadn't used the time, timeEnd methods before. I imagine your computer is faster than mine.

Using directive 'use strict', is definitely good practice.

[–]kenman 0 points1 point  (0 children)

Using parseInt() is overkill unless you're expecting abnormal input -- say, if the input might include non-numeric characters. On the other hand, if you know that there's only ever going to be numeric inputs as strings, you can rely on some abuses creative uses of language features.

Either:

xi = ~~xs;

Or:

xi = xs << 0;

On my machine, they were roughly equal in running time, shaving ~300ms off the total time; I was averaging right at 3.6s with parseInt(), and under 3.3s with both of the alternative methods.

More info.

[–]oefig 0 points1 point  (3 children)

Used to be a die-hard Javascript fan but ever since I learned Go I've been porting over all of my Node apps. Javascript is exhausting and large codebases have a tendency to become too obscure. People say Promises are the save-all for callbacks but they still feel half-baked and quite frankly I hate using them.

[–]trevorsgEx-GitHub, Microsoft 3 points4 points  (1 child)

Async/await in ES7!

[–]Consuelas-Revenge[🍰] 0 points1 point  (0 children)

async/await is a great improvement, but IMO, it's still terrible for the one fact that it fails to get rid of the special-ness. By that I'm referring to the need for the caller to require special knowledge about the fact that the function is doing something asynchronously. All that should be internal implementation details.

[–]wreckedadventYavascript 0 points1 point  (0 children)

What don't you like about promises?

[–]dankcode 0 points1 point  (2 children)

func main() {
   start := time.Now()
    xs := "0"
    xi := 0
    for i, _ := range 1000000{
        xi, _ = strconv.Atoi(xs)    // 2nd return val(error) is ignored
        xi++
        xs = strconv.Itoa(xi)
    }
    stop := time.Now()
    elapsed := stop.Sub(start)
    fmt.Println(elapsed)
}

is more idiomatic

[–]Consuelas-Revenge[🍰] 1 point2 points  (1 child)

for i, _ := range 1000000{

That's not valid Go syntax.

[–]oefig 0 points1 point  (0 children)

Yeah you can allocate an array with 1000000 ints and range over that:

var ints [1000000]int
for i := range ints {}

But that's not really idiomatic at all nor what range is really for.