How fast is indexof as3




















A Function parameter specifies a comparison method. A Number value specifies the sorting options. Adds elements to and removes elements from the Vector. Note: To override this method in a subclass of Vector, use Parameters startIndex : int — An integer that specifies the index of the element in the Vector where the insertion or deletion begins. This number includes the element specified in the startIndex parameter. If the value is 0, no elements are deleted. Returns a string that represents the elements in the specified Vector.

Every element in the Vector, starting with index 0 and ending with the highest index, is converted to a concatenated string and separated by commas. In the ActionScript 3. Returns a string that represents the elements in the Vector.

To specify a custom separator, use the Vector. Adds one or more elements to the beginning of the Vector and returns the new length of the Vector. Filters: Retrieving Data from Server Retrieving Data from Server Classes x. The data type of a Vector's elements is known as the Vector's base type. The base type can be any class, including built in classes and custom classes. The base type is specified when declaring a Vector variable as well as when creating an instance by calling the class constructor.

Creating arrays Creating a Vector instance Inserting array elements Retrieving values and removing array elements Sorting an array Querying an array Arrays example: PlayList. Working with arrays Basics of arrays Indexed arrays Associative arrays Multidimensional arrays Cloning arrays. Public Properties. Hide Inherited Public Properties. Show Inherited Public Properties. A reference to the class object or constructor function for a given object instance. Public Methods. Hide Inherited Public Methods.

Show Inherited Public Methods. Indicates whether an object has a specified property defined. Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter.

Indicates whether the specified property exists and is enumerable. Sets the availability of a dynamic property for loop operations. Property Detail. Implementation public function get fixed : Boolean public function set fixed value: Boolean : void.

Implementation public function get length : uint public function set length value: uint : void Throws RangeError — If this property is changed while fixed is true. RangeError — If this property is set to a value larger than the maximum allowable index 2 Copy var v:Vector.

AS3 function concat Returns Vector. Throws TypeError — If any argument is not a Vector of the base type, or cannot be converted to a Vector of the base type. More examples Querying an array.

However, suppose you create a function on a frame on the main timeline using Flash Professional, but you want it to be called in a different this context: function myFunction item:T, index:int, vector:Vector. Returns Boolean — A Boolean value of true if the specified function returns true when called on all items in the Vector; otherwise, false.

Returns int — A zero-based index position of the item in the Vector. If the searchElement argument is not found, the return value is Returns String — A string consisting of the elements of the Vector converted to strings and separated by the specified string.

However, suppose you create a function on a frame on the main timeline, using Flash Professional but you want it to be called in a different this context: function myFunction item:Object, index:int, vector:Vector.

The result Vector has the same base type and length as the original. Returns T — The value of the last element in the specified Vector. Throws RangeError — If this method is called while fixed is true. More examples Retrieving values and removing array elements. AS3 function push Returns uint — The length of the Vector after the new elements are added. RangeError — If this method is called while fixed is true. More examples Inserting array elements. Returns T — The element that was removed from the original Vector.

Throws RangeError — If the index argument specifies an index to be deleted that's outside the Vector's bounds. AS3 function reverse : Vector. More examples Sorting an array. Returns T — The first element in the Vector. Throws RangeError — If fixed is true. However, suppose you create a function on a frame on the main timeline, but you want it to be called in a different this context: function myFunction item:Object, index:int, vector:Vector.

Returns Boolean — A Boolean value of true if any items in the Vector return true for the specified function; otherwise, false. Related API Elements every. A Vector instance is a typed array , which means that all the elements in a Vector instance always have the same data type. When you declare a Vector variable or instantiate a Vector object, you explicitly specify the data type of the objects that the Vector can contain.

At run time and at compile time in strict mode , any code that sets the value of a Vector element or retrieves a value from a Vector is checked. In addition to the data type restriction, the Vector class has other restrictions that distinguish it from the Array class:.

A Vector is a dense array. An Array object may have values in indices 0 and 7 even if it has no values in positions 1 through 6. However, a Vector must have a value or null in each index. A Vector can optionally be fixed-length. You can never read a value from an index greater than the final element length - 1. You can never set a value with an index more than one beyond the current final index.

In other words, you can only set a value at an existing index or at index [length]. As a result of its restrictions, a Vector has three primary benefits over an Array instance whose elements are all instances of a single class:.

Performance: array element access and iteration are much faster when using a Vector instance than when using an Array instance.

Type safety: in strict mode the compiler can identify data type errors. Examples of such errors include assigning a value of the incorrect data type to a Vector or expecting the wrong data type when reading a value from a Vector. At run time, data types are also checked when adding data to or reading data from a Vector object. When using those methods the values are still checked at run time. Reliability: runtime range checking or fixed-length checking increases reliability significantly over Arrays.

Aside from the additional restrictions and benefits, the Vector class is very much like the Array class. The properties and methods of a Vector object are similar—for the most part identical—to the properties and methods of an Array. In most situations where you would use an Array in which all the elements have the same data type, a Vector instance is preferable. You can use several techniques to create an Array instance or a Vector instance.

However, the techniques to create each type of array are somewhat different. You create an Array object by calling the Array constructor or by using Array literal syntax. The Array constructor function can be used in three ways. First, if you call the constructor with no arguments, you get an empty array.

You can use the length property of the Array class to verify that the array has no elements. For example, the following code calls the Array constructor with no arguments:. The argument must be an unsigned integer between the values 0 and 4,,, For example, the following code calls the Array constructor with a single numeric argument:. Third, if you call the constructor and pass a list of elements as parameters, an array is created, with elements corresponding to each of the parameters.

The following code passes three arguments to the Array constructor:. You can also create arrays with Array literals. An Array literal can be assigned directly to an array variable, as shown in the following example:.

You create a Vector instance by calling the Vector. You can also create a Vector by calling the Vector. That function converts a specified object to a Vector instance. In Flash Professional CS5 and later, Flash Builder 4 and later, and Flex 4 and later, you can also create a vector instance by using Vector literal syntax.

Any time you declare a Vector variable or similarly, a Vector method parameter or method return type you specify the base type of the Vector variable.

You also specify the base type when you create a Vector instance by calling the Vector. Put another way, any time you use the term Vector in ActionScript, it is accompanied by a base type. The type parameter immediately follows the word Vector in the code. It consists of a dot. In the first line of the example, the variable v is declared as a Vector. In other words, it represents an indexed array that can only hold String instances. The second line calls the Vector constructor to create an instance of the same Vector type that is, a Vector whose elements are all String objects.

It assigns that object to v. If you use the Vector. You can test that a Vector is empty by checking its length property.

For example, the following code calls the Vector. If you know ahead of time how many elements a Vector initially needs, you can pre-define the number of elements in the Vector.

To create a Vector with a certain number of elements, pass the number of elements as the first parameter the length parameter. If the base type is a reference type that allows null values, the elements all contain null. Otherwise, the elements all contain the default value for the class.

Consequently, in the following code listing the Vector named ages is created with seven elements, each containing the value Finally, using the Vector. Note, however, that you can still change the values of the elements of a fixed-length Vector. Empty items in the array are not supported; a statement such as var v:Vector. You can't specify a default length for the Vector instance.

Instead, the length is the same as the number of elements in the initialization list. You can't specify whether the Vector instance has a fixed length.

Instead, use the fixed property. In addition to the Vector. The Vector. When you call the Vector. You pass a single indexed array Array or Vector instance as an argument. The method then returns a Vector with the specified base type, containing the values in the source array argument. The following code listing shows the syntax for calling the Vector.

First, when an Array instance is passed to the function, a Vector instance is returned. The conversion uses standard ActionScript data type conversion rules. For example, the following code listing converts the String values in the source Array to integers in the result Vector. The decimal portion of the first value "1. When code calls the Vector. Using the Vector.

The most basic way to add an element to an indexed array is to use the array access [] operator. To set the value of an indexed array element, use the Array or Vector object name and index number on the left side of an assignment statement:. If a value exists at that index, the new value replaces the existing one. An Array object allows you to create an element at any index. However, with a Vector object you can only assign a value to an existing index or to the next available index.

The safest way to add a new element to a Vector object is to use code like this listing:. They simply loop from one end of the string to the other looking for a given character code. The rest is just a bit of error case handling. Note that these are different from String.

So keep in mind that this function is therefore quite a bit simpler and should be more efficient when you see the performance results later. Launch the test app. These results are pretty clear. The charCodeAt -based approaches are over twice as fast when the character code to find is right at the beginning of the search and over twice as slow when the character code to find is all the way at the end.

As the work done by the function increases this overhead becomes relatively less important and the native code implementation beats the AS3 version handily.

In some special cases like indexOf for just a character code that you expect to be right at the front of the String there are optimizations to be had. Know any other functions with reduced overhead? Spot a bug?



0コメント

  • 1000 / 1000