Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

How do I optimize this (node.js)?

Name: Anonymous 2012-05-25 16:51

The loop seems unnecessary but I don't know how to get rid of it.


var a = [
            "Start ",
            function(){return "middle "},
            "end"
        ],
    c = "";
for(var i = 0; i < a.length; i++){
    var d = typeof a[i] === 'function' ? a[i]() : a[i];
    c = c.concat(d);
}
console.log(c);


var a is generated once at runtime from an array of strings and another array of functions. The functions obviously return dynamic content so doing something like this var a = a[0]+a[1]()+a[2]; by loop once at runtime is not an option since I'd be saving the result of the function and not the function itself.

I've seen some code where a tailor-made function function(){return a[0]+a[1]()+a[2];} is created at runtime that returns the strings and functions() correctly but I don't know how to achieve something like that. In the example I saw the JavaScript source stored in a "string" and editted via String methods and then eval() evaluated. I don't even know if it's worth it in that case.

Thoughts? Help?!

Name: Anonymous 2012-05-26 21:07

JavaScript maps are shit. The only reason for you to use them is to beautify your code. But on average they are slower than a simply for loop like OP's.

I'd speculate that it's because of the extra function call.

Name: Anonymous 2012-05-26 22:16

>>21

I'm not sure if it would apply to >>8 and >>10, but it could also be the allocation of intermediate arrays that can other wise be avoided.

like the difference between:

array.map(f).map(g).map(h)

and

array.map(compose(f,g,h))

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List