Note, I've modified the posted script so the below "issue" is properly handled, So, the below comments are no longer accurate...
Also, note this is only 100% accurate if each array contains unique items. If you pass in 2 arrays that contain the same number of elements, but different quantities of common elements, it'll get fooled.
For instance, it'll return
true on these 2 arrays...
var a1 = [1, 1, 1, 2]
var a2 = [2, 2, 2, 1]
Because they both have 4 elements and the elements consist of only the integer values 1 and 2. It will not detect the differing quantities of duplicated elements.
But, if you don't have duplicate items in your arrays, you should be fine.
If you do, the algorithm would need to be improved to account for that.