Apr 06 2009

Actionscript Vector class initialization with a source Array

Category: actionscriptDavide Zanotti @ 8:35 am

What you maybe already know:

Vector class is a powerful class introduced with flash player 10. It is fundamentally a special typed array into which all elements must be the same type and it is faster than a normal Array.

What you maybe don’t know:

It’s possible to initialize a Vector with an Array or another Vector as a source but you must use the Vector function not the class constructor. The class constructor in fact has the following signature:

1
Vector(length:uint = 0, fixed:Boolean = false)

Vector function instead has this:

1
Vector(sourceArray:Object):Vector.<T>

So, rather than using such approach:

1
2
3
4
5
var vector:Vector.<String> = new Vector.<String>();
vector[0] = "value1";
vector[1] = "value2";
vector[2] = "value3";
// ...and so on

We can do the following (Notice the absence of “this” keyword):

1
2
3
4
5
6
7
8
9
// create a vector with an inline Array as source
var vector:Vector.<String> = Vector.<String>(["v1", "v2", "v3"]);

// create a vector with an inline Vector as source
var vector2:Vector.<String> = Vector.<String>(vector);

// Testing (both trace will print the same string)
trace(vector[1]);
trace(vector2[1]);

Tags: , , ,


Dec 09 2008

Javascript string concatenation performance on modern browsers: array.join("") is now slower!

Category: javascriptDavide Zanotti @ 3:52 pm

Well… as many Javascript developers know, a common practice to handle big string concatenation, is to implement a sort of  “stringBuffer” by using an array as data container and then convert it to a string by using the method join(). Ie:

var buffer = [];
var target = document.getElementById("box");

for (var i=0; i<10000; i++) {
    buffer .push(['<p id="node_', i, '" id="node_', i, '">Content ', i, '</p>'].join(""));
}

target.innerHTML = buffer.join("");

This is theoretically the fastest way to manage huge strings and to speed up the building of dynamic content.

Continue reading “Javascript string concatenation performance on modern browsers: array.join("") is now slower!”

Tags: , , , , , , , , , , ,