JS Speed

goto10's icon

Hey- trying to integrate JS and Max for the first time. Any reason this would be going as slow as it is? It brings the framerate down to like 3 fps. Any suggestions?

Thanks!

-Jesse`

Max Patch
Copy patch and select New From Clipboard in Max.

`

4289.hello.js
js
goto10's icon

Does it have to do with the fact I'm sending something to an outlet instead of talking to the sketch object internally?

Anthony Palomba's icon

I am very curious about this as well. When Max6 came out, there was great fanfare
regarding the improved js performance.

When I run your test patch in Max6, the performance starts out good but then within a
few seconds drops to 3fps. It also maxes out the CPU on my quadcore MacBook Pro.

It would be great to have some kind of guidelines as to the best way to use JS in
a patch. What is the best way pass around data or messages from JS to objects?

sbl's icon

A couple of things:

You don't need to create an array to send things out of an outlet.outlet(0, x, y, z) is just fine. this will get rid of the "fromsymbol" in the patcher as well.

The creation of the tempArrays is what is slowing down your code a lot (you are creating about 80 Array objects a second). Check the attached patcher for a better solution. At least the framerate is stable.

If performance is key I would rather create jitter objects from javascript code and connect them. They will always be more optimized than any js code.

this.autowatch = 1;
this.outlets = 1;
this.inlets = 1;

var step = 0.05,
    lastx = -2,
    lasty = -2,
    y = 0.5,
    borderx = -2,
    bordery = 0.1;

function bang() {
    lastx = -2;
  lasty = 0;

    this.outlet(0, "reset");
    this.outlet(0, "glcolor", 1, 1, 1, 1);
    for (var x = borderx; x  -2) {
            lineseg(x, y, 0, lastx, lasty, 0);
        }
        lastx = x,
        lasty = y;
    }
}

function lineseg(x1, y1, z1, x2, y2, z2) {
    this.outlet(0, "linesegment", x1, y1, z1, x2, y2, z2);
}

goto10's icon

Great- thanks for the advice! Yeah- I thought the array thing would be a clever way to clean up what I was sending to the outlet, without having to put in quotes and spaces. But I guess I was wrong!

Do you know if there is a speed difference between creating JitterObjects and sending them commands in JS vs creating the same Max Objects in the patch, referencing them in JS with something like patcher.getNamed("") and sending them messages vs sending messages out of the outlets?

While on these lines, do you know if anyone has given any thought to implementing Google's v8 interpreter? Seems like its 8-10x as fast as spidermonkey. How hard would it be to make an external that references and autowatches a js file or an mxj with something like jav8? I only ask because I've recently become enamored of OpenEndedGroup's Field project. Was hoping something like that might be possible within Max, but it would really rely on the speed of the JavaScript interpreter.

Thanks again for your help!

-Jesse