How to determine if a string is a permutation of another string in JavaScript

Here is one way to determine whether a string is a permuation of another string in JavaScript.
Space complexity: O(n)
Time complexity: O(n)

Another approach could involve sorting the two strings and iterating through the length of one of the strings.

checkPermutationTry in REPL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function checkPermutation(string1, string2) {
const charCount1 = getCountMap(string1);
const charCount2 = getCountMap(string2);
return charCountsMatch(charCount1, charCount2);
}
function getCountMap(string) {
return string.split('').reduce((map, char) => {
(map[char] && map[char]++) || (map[char] = 1);
return map;
}, {});
}
function charCountsMatch(map1, map2) {
if (Object.keys(map1).length !== Object.keys(map2).length) return false;
for (const key in map1) {
if (map1[key] !== map2[key]) return false;
}
return true;
}
// ========
function checkPermutationBitwise(string1, string2) {
let signature1, signature2 = 0;
string1.split('').forEach((char) => {
signature1 ^= char.charCodeAt(0);
});
string2.split('').forEach((char) => {
signature2 ^= char.charCodeAt(0);
});
return signature1 === signature2;
}
// checkPermutation = checkPermutationBitwise // Uncomment to use bitwise version

Tests can be found at REPL.it