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

Pages: 1-

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-25 17:41

Read SICP, etc.

Name: Anonymous 2012-05-25 18:39

You optimize it by not using JS, fagstorm.

Name: Anonymous 2012-05-25 20:12

For each function that is created, create a run string that contains the name and the data inside.

' a = [ "begin", function() { return "middle"}, "end ]; a();'

Join all of those run strings together, then do an eval on the result. May want to add a = null at the end to keep the functions from piling up or...something.

How's that?

Name: Anonymous 2012-05-25 20:15

>>4
using eval

Name: Anonymous 2012-05-25 20:17

>>5
You eval the running strings after joining them together. OP called for a way using eval.

Name: Anonymous 2012-05-25 20:24

>>6
using eval in javascript

Name: Anonymous 2012-05-25 21:41

ffs

Don't bother optimizing unless you've got actual numbers showing this code is a performance bottleneck. Moreover, eval tends to confuse JavaScript runtimes and defeat the normal optimizations they're able to do, so treat it as a last resort.

So here is a superior FUNCTIONAL version of your code instead.

var a = [
            "Start ",
            function(){return "middle "},
            "end"
        ],
    c = a.map(function(value) {
        return typeof value === 'function' ? value() : value;
    }).join();
console.log(c);

Name: Anonymous 2012-05-25 22:21

not touhou

Name: Anonymous 2012-05-25 23:18

>>8
String.prototype.call = function(){ return this; };
var a = [
            "Start ",
            function(){return "middle "},
            "end"
        ],
    c = a.map(function(x){ return x.call(); }).join();
console.log(c);

Name: Anonymous 2012-05-25 23:31

>>10
lol

Name: Anonymous 2012-05-26 0:40

>>10
This actually reminds me of some Haskell code I've seen...
instance (Fractional a) => Fractional (x -> a) where
    f / g = liftM2 (/) f g
    recip = (recip .)
    fromRational = const . fromRational

average = sum / genericLength

fib = (round .) $ ap ((/) . (**) . (1 +)) ((. (2 **)) . (*)) $ sqrt 5

Name: Anonymous 2012-05-26 3:36

>>10
this is optimal if all you know is that the array consists of mixed strings and functions that return strings. A routine for concatenating nested arrays of strings can be optimized with a two pass approach, where the first pass calculates the sum of the lengths of all contained strings, allocates a buffer of this length, and then writes all strings to the buffer in a depth first traversal.

Name: Anonymous 2012-05-26 3:45

>>1
Rewrite it in C++

Name: Anonymous 2012-05-26 16:11

use javascript

Name: Lambda 2012-05-26 18:43

>>12
UMENA!!!!!!!!!!!!!!
I LOVE YOU!!

Name: Anonymous 2012-05-26 18:53

>>8: 490 ms for 100000 array elements.
>>10: 171 ms for 100000 array elements.

lol

Name: Anonymous 2012-05-26 18:58

>>17
benchmark code: data:text/html;charset=utf-8;base64,PCFkb2N0eXBlIGh0bWw%2BDQo8aHRtbD4NCiA8aGVhZD4NCiAgPHRpdGxlPnNpbXBsZSBiZW5jaG1hcms8L3RpdGxlPg0KICA8c2NyaXB0Pg0KDQpmdW5jdGlvbiBlaWdodChhKXsNCiAgcmV0dXJuIGEubWFwKGZ1bmN0aW9uKHZhbHVlKSB7DQogICAgICAgICAgIHJldHVybiB0eXBlb2YgdmFsdWUgPT09ICdmdW5jdGlvbicgPyB2YWx1ZSgpIDogdmFsdWU7DQogICAgICAgICB9KS5qb2luKCk7DQp9DQoNCmZ1bmN0aW9uIHRlbihhKXsNCiAgcmV0dXJuIGEubWFwKGZ1bmN0aW9uKHgpeyByZXR1cm4geC5jYWxsKCk7IH0pLmpvaW4oKTsNCn0NCg0KZnVuY3Rpb24gbWFrZV9hcnJheShuKXsNCiAgdmFyIHJldCA9IG5ldyBBcnJheSgpOw0KICBmb3IodmFyIGkgPSAwOyBpIDwgbjsgKytpKXsNCiAgICByZXQucHVzaChNYXRoLnJvdW5kKE1hdGgucmFuZG9tKCkpID8NCiAgICAgICAgICAgICAgIG5ldyBGdW5jdGlvbigncmV0dXJuICcgKyBpLnRvU3RyaW5nKCkgKyAnOycpIDoNCiAgICAgICAgICAgICAgIGkudG9TdHJpbmcoKSk7DQogIH0NCiAgcmV0dXJuIHJldDsNCn0NCg0KZnVuY3Rpb24gYmVuY2gobil7DQogIHZhciBhID0gbWFrZV9hcnJheShuKQ0KICB2YXIgc3RhcnQgPSAobmV3IERhdGUoKSkuZ2V0VGltZSgpOw0KICBlaWdodChhKTsNCiAgdmFyIG1pZCA9IChuZXcgRGF0ZSgpKS5nZXRUaW1lKCk7DQogIFN0cmluZy5wcm90b3R5cGUuY2FsbCA9IGZ1bmN0aW9uKCkgeyByZXR1cm4gdGhpczsgfTsNCiAgdGVuKGEpOw0KICB2YXIgZW5kID0gKG5ldyBEYXRlKCkpLmdldFRpbWUoKTsNCiAgZG9jdW1lbnQuYm9keS5pbm5lckhUTUwgPSAnPj44OiAnICsgKG1pZCAtIHN0YXJ0KSArICcgbXMgZm9yICcgKyBuICsNCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAnIGFycmF5IGVsZW1lbnRzLjxicj4%2BPjEwOiAnICsgKGVuZCAtIG1pZCkgKw0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICcgbXMgZm9yICcgKyBuICsgJyBhcnJheSBlbGVtZW50cy4nOw0KfQ0KDQogIDwvc2NyaXB0Pg0KIDwvaGVhZD4NCiA8Ym9keSBvbmxvYWQ9ImJlbmNoKDEwMDAwMCkiPjwvYm9keT4NCjwvaHRtbD4%3D

Name: Anonymous 2012-05-26 19:55

>>17
if javscript could into lazy maps, then it could be just as fast.

Name: >>19 2012-05-26 20:06

typeof value === 'function'

oh, nevermind.

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))

Name: Anonymous 2012-05-27 18:38

>>21-22
a simple modification to >>10:
String.prototype.call = function(){ return this; };
var a = [
            "Start ",
            function(){return "middle "},
            "end"
        ],
    f = function(x){ return x.call(); },
    c = a.map(f).join();
console.log(c);


makes it just as fast as using a for loop:
String.prototype.call = function(){ return this; };
var a = [
            "Start ",
            function(){return "middle "},
            "end"
        ],
    c = '';
for(var i = 0; i < a.length; ++i) c += a[i].call();
console.log(c);


for 100000 array elements:
map: 56 ms
for loop: 52 ms

Name: Anonymous 2012-05-27 18:42

Japanese Imperialist propaganda in localised Crash Bandicoot game:
http://www.youtube.com/watch?v=Ud8I0S6zr8A

Name: bampu pantsu 2012-05-29 5:10

bampu pantsu

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