<!--your preparation HTML code goes here-->
const DATA = {
'#': '#',
$: '$',
'%': '%',
'&': '&',
"'": ''',
'(': '(',
')': ')',
'+': '+',
';': ';',
'^': '^',
À: 'À',
Á: 'Á',
Â: 'Â',
Ã: 'Ã',
Ä: 'Ä',
Å: 'Å',
Æ: 'Æ',
Ç: 'Ç',
È: 'È',
'~': '~',
'¡': '¡',
'¢': '¢',
'£': '£',
'¤': '¤',
'¥': '¥',
'¦': '¦',
'§': '§',
'¨': '¨',
'©': '©',
ª: 'ª',
'«': '«',
'¬': '¬',
'•': '­',
'®': '®',
'¯': '¯',
'°': '°',
'±': '±',
'²': '²',
'³': '³',
'´': '´',
µ: 'µ',
'¶': '¶',
'·': '·',
'¸': '¸',
'¹': '¹',
º: 'º',
'»': '»',
'¼': '¼',
'½': '½',
'¾': '¾',
'¿': '¿',
É: 'É',
Ê: 'Ê',
Ë: 'Ë',
Ì: 'Ì',
Í: 'Í',
Î: 'Î',
Ï: 'Ï',
Ð: 'Ð',
Ñ: 'Ñ',
Ò: 'Ò',
Ó: 'Ó',
Ô: 'Ô',
Õ: 'Õ',
Ö: 'Ö',
'×': '×',
Ø: 'Ø',
Ù: 'Ù',
Ú: 'Ú',
Û: 'Û',
Ü: 'Ü',
Ý: 'Ý',
Þ: 'Þ',
ß: 'ß',
à: 'à',
á: 'á',
â: 'â',
ã: 'ã',
ä: 'ä',
å: 'å',
æ: 'æ',
ç: 'ç',
è: 'è',
é: 'é',
ê: 'ê',
ë: 'ë',
ì: 'ì',
í: 'í',
î: 'î',
ï: 'ï',
ð: 'ð',
ñ: 'ñ',
ò: 'ò',
ó: 'ó',
ô: 'ô',
õ: 'õ',
ö: 'ö',
'÷': '÷',
ø: 'ø',
ù: 'ù',
ú: 'ú',
û: 'û',
ü: 'ü',
ý: 'ý',
þ: 'þ',
ÿ: 'ÿ',
Ā: 'Ā',
ā: 'ā',
Ă: 'Ă',
ă: 'ă',
Ą: 'Ą',
'’': '’',
'‐': '‐',
à: 'à',
'<': '<',
'>': '>',
}
const charMap = new Map(Object.entries(DATA));
const esapiEncoder = (text) => {
if (!text) return '';
let newText = '';
for (let i = 0, e = text.length; i < e; i++) {
const char = text[i];
newText += charMap.get(char) ?? char;
}
return newText;
};
esapiEncoder('OIFJOSIJFOIJFIOSJFOSIDJFOISJDFOISJFIO>')
const esapiEncoder = (text) => {
if (!text) return '';
let newText = '';
for (let i = 0, e = text.length; i < e; i++) {
const char = text[i];
newText += DATA[char] ?? char;
}
return newText;
};
esapiEncoder('OIFJOSIJFOIJFIOSJFOSIDJFOISJDFOISJFIO>')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
MAP | |
OBJ |
Test name | Executions per second |
---|---|
MAP | 429008.0 Ops/sec |
OBJ | 180067.7 Ops/sec |
The benchmark defined in the provided JSON compares two approaches for encoding characters using a mapping: one approach employs JavaScript's Map
object, while the other uses a standard JavaScript object (or literal) for the same purpose. The benchmark aims to measure the performance of these two methods in a character encoding scenario. Below is a detailed explanation of the options being compared, along with their pros and cons, as well as some additional considerations.
Using a JavaScript Map
:
const esapiEncoder = (text) => {
if (!text) return '';
let newText = '';
for (let i = 0, e = text.length; i < e; i++) {
const char = text[i];
newText += charMap.get(char) ?? char;
}
return newText;
};
Map
called charMap
stores the character mappings. The encoder function retrieves the encoded value using the get
method of Map
.Using a Plain JavaScript Object (OBJ):
const esapiEncoder = (text) => {
if (!text) return '';
let newText = '';
for (let i = 0, e = text.length; i < e; i++) {
const char = text[i];
newText += DATA[char] ?? char;
}
return newText;
};
DATA
) is used to store the character mappings. The encoder accesses the encoded value directly via bracket notation.From the benchmark results:
MAP
approach achieved 429,007.97 Executions Per Second.OBJ
approach achieved 180,067.66 Executions Per Second.This indicates that the Map
method performed significantly better than the standard object method in this benchmark scenario, validating the theory that Maps generally offer better performance for dynamic use cases, especially when accessing data frequently in a loop.
Map
vs. Object
can depend on the specific use case—if the dataset is static and known in advance, an object may suffice and be more memory-efficient. However, if flexibility or frequent updates are required, using a Map
might be more appropriate.Map
is widely supported across modern browsers, so developers usually don’t have to worry about compatibility issues unless they target very outdated environments.In conclusion, the benchmark provides insights into the performance differences between two common ways of handling character mappings in JavaScript, and helps in understanding when to use each approach based on the requirements of the application.