Are there anything like js_es=6 clone array?

Are there anything like js_es=6 clone array?

 this.result.push([...a]);

the performance of this is faster than array.slice(0);

line : 48 for test
[…a] the fastest is 508ms
a.slice(0) is 604ms

	// Generated by Haxe 4.0.0-rc.5+b1fb4afca
	(function ($global) { "use strict";
	class HxOverrides {
		static iter(a) {
			return { cur : 0, arr : a, hasNext : function() {
				return this.cur < this.arr.length;
			}, next : function() {
				return this.arr[this.cur++];
			}};
		}
	}
	class Main {
		static main() {
			console.log("src/Main.hx:6:","Hello, world!");
			var total = 10;
			var _g = [];
			var _g1 = 1;
			var _g2 = total + 1;
			while(_g1 < _g2) {
				var i = _g1++;
				_g.push(i);
			}
			var sequence = _g;
			var start = new Date().getTime();
			var m = new Permutation(sequence,function(a) {
				console.log("src/Main.hx:12:","spend time=" + (new Date().getTime() - start));
				console.log("src/Main.hx:13:",a.length);
				console.log("src/Main.hx:15:","a---");
				console.log("src/Main.hx:17:",a.slice(0,10));
			});
		}
	}
	class Permutation {
		constructor(arr,callBack) {
			this.result = [];
			var i = arr.length;
			var total = 1;
			while(i > 0) {
				total *= i;
				--i;
			}
			this.total = total;
			this.callBack = callBack;
			this.heapPermutation(arr,arr.length,arr.length);
		}
		heapPermutation(a,size,n) {
			if(size == 1) {
				this.result.push([...a]);
				if(this.result.length == this.total) {
					this.callBack(this.result);
				}
			}
			var _g = 0;
			var _g1 = size;
			while(_g < _g1) {
				var i = _g++;
				this.heapPermutation(a,size - 1,n);
				if(size % 2 == 1) {
					var temp = a[0];
					a[0] = a[size - 1];
					a[size - 1] = temp;
				} else {
					var temp1 = a[i];
					a[i] = a[size - 1];
					a[size - 1] = temp1;
				}
			}
		}
	}
	Main.main();
	})({});

What about a simple array.copy()?

You can always build this yourself:

using Test;
class Test {
  static function main() {
    var arr = [1,2,3];
    arr.spread([4,5,6]);
    trace(arr);
  }
  
  static inline function spread<T>(array:Array<T>, values:Array<T>) return {
   	js.Syntax.code("{0}.push([...{1}])", array, values);
  }
}

or use untyped __js__ instead of js.Syntax.code if you are on haxe 3

1 Like

thank you ,I’ll try .