-
Version: 3.0.0-beta-2050 Array performance has come up before, see #194 and #832, but it still seems to be an issue. Accessing arrays seems to be very slow. This is mostly notable if you have to loop over big arrays many times. Take this code for example: using Jint;
var engine = new Engine();
engine.SetValue("log", new Action<object>(Console.WriteLine));
engine.SetValue("random", new Random(123)); // Set seed to get the same results every time
engine.Execute(@"
class Point
{
constructor(x, y, z)
{
this.x = x;
this.y = y;
this.z = z;
}
}
// Generate lots of points with random-ish x, y, and z values
const allPoints = [];
for(let i=0; i < 20000; i++)
allPoints.push(new Point(random.next(25), random.next(25), random.next(25)));
const uniquePoints = []
const start = new Date();
// Go through all points and add it to the list of unique points if the x, y, and z
// values have not been seen before
allPoints.forEach(p => {
if(!uniquePoints.some(e => e.x === p.x && e.y === p.y && e.z === p.z))
uniquePoints.push(p);
});
const elapsed = (new Date()).getTime() - start.getTime();
log(`There are ${uniquePoints.length} unique points. Finding them took ${elapsed} ms`);
"); It creates an array that has 20000 points with semi-random x, y, and z values (the seed has been set, so it'll always generates the same ones). It then finds points with x, y, and z positions that have not been seen yet, and adds them to an array. The slowness comes from having to loop over On my PC it runs for around 33 seconds. I adapted the script for NodeJS, and there it takes around 100 milliseconds. While I of course do not expect V8 performance a factor of 330 seems a bit wild. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Would you like to help out with performance improvements or should we just convert this to discussion trying to find optimal array access patterns? |
Beta Was this translation helpful? Give feedback.
-
Here's a version of the sample written with performance in mind. class Point
{
constructor(x, y, z)
{
this.x = x;
this.y = y;
this.z = z;
}
}
// Generate lots of points with random-ish x, y, and z values
const lookup = new Set();
const allPoints = [];
for(let i=0; i < 20000; i++) {
const point = new Point(random.next(25), random.next(25), random.next(25));
allPoints.push(point);
}
const uniquePoints = [];
const start = new Date();
// Go through all points and add it to the list of unique points if the x, y, and z
// values have not been seen before
allPoints.forEach(p => {
const key = p.x + '-' + p.y + '-' + p.z;
if (!lookup.has(key)) {
uniquePoints.push(p);
lookup.add(key);
}
});
const elapsed = (new Date()).getTime() - start.getTime();
log(`There are ${uniquePoints.length} unique points. Finding them took ${elapsed} ms`);
|
Beta Was this translation helpful? Give feedback.
Here's a version of the sample written with performance in mind.