Forums > Javascript

JS Speed

August 7, 2012 | 10:26 pm

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`

– Pasted Max Patch, click to expand. –

`

Attachments:
  1. hello.js

August 8, 2012 | 3:46 pm

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


August 9, 2012 | 7:55 pm

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
August 10, 2012 | 8:22 am

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; x += step) {
		y = bordery + (0.5 - Math.random());
		if (lastx > -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);
}

August 10, 2012 | 5:38 pm

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


Viewing 5 posts - 1 through 5 (of 5 total)