Overall feedback
It seems there are quite a few global variables referenced within various functions. This isn't totally bad but it makes things difficult - like unit testing. If functions accepted parameters and returned certain output then testing might be easier.
The answer by Ted Brownlow suggests using a Plain-old Javascript object (A.K.A. a POJO) to store the occurrences instead of an array - i.e. a mapping of characters to counts. This can eliminate the need to initialize the array and set all values to zero.
You may be interested to read other posts involving Huffman Encoding, including this one.
Suggestions
Initializing an array of zeroes
In the function count()
there is this code:
occurences = new Array(128); // Initialize with zero for (let i = 0; i < occurences.length; i++) { occurences[i] = 0; }
The loop can be avoided by using array.fill()
.
Excess variables
in the function isASCII
there is the variable test
that is returned immediately after being assigned. While this may be leftover from debugging the variable can be eliminated. The whole function could be expressed as a one-line arrow function.
Avoid excess DOM lookups
The code in huffman()
accesses DOM elements each time. While it may not be as much of an issue with todays browsers, it is wise to cache DOM references one they are available (e.g. in the DOMContentLoaded
event).
"...DOM access is actually pretty costly - I think of it like if I have a bridge - like two pieces of land with a toll bridge, and the JavaScript engine is on one side, and the DOM is on the other, and every time I want to access the DOM from the JavaScript engine, I have to pay that toll"
- John Hrvatin, Microsoft, MIX09, in this talk Building High Performance Web Applications and Sites at 29:38, also cited in the O'Reilly Javascript book by Nicholas C Zakas Pg 36, as well as mentioned in this post
Alerts
There are two places alert()
is called (one in huffman()
and one in the window.onerror
handler). This can be an issue because some users may have disabled alerts in a browser setting. It is better to use HTML5 <dialog>
element - it allows more control over the style and doesn't block the browser. Bear in mind that it isn't supported by IE and Safari (And seemingly Chrome on iOS) but there is a polyfill
Overall feedback
It seems there are quite a few global variables referenced within various functions. This isn't totally bad but it makes things difficult - like unit testing. If functions accepted parameters and returned certain output then testing might be easier.
The answer by Ted Brownlow suggests using a Plain-old Javascript object (A.K.A. a POJO) to store the occurrences instead of an array - i.e. a mapping of characters to counts. This can eliminate the need to initialize the array and set all values to zero.
You may be interested to read other posts involving Huffman Encoding, including this one.
Suggestions
Initializing an array of zeroes
In the function count()
there is this code:
occurences = new Array(128); // Initialize with zero for (let i = 0; i < occurences.length; i++) { occurences[i] = 0; }
The loop can be avoided by using array.fill()
.
Excess variables
in the function isASCII
there is the variable test
that is returned immediately after being assigned. While this may be leftover from debugging the variable can be eliminated. The whole function could be expressed as a one-line arrow function.
Avoid excess DOM lookups
The code in huffman()
accesses DOM elements each time. While it may not be as much of an issue with todays browsers, it is wise to cache DOM references one they are available (e.g. in the DOMContentLoaded
event).
"...DOM access is actually pretty costly - I think of it like if I have a bridge - like two pieces of land with a toll bridge, and the JavaScript engine is on one side, and the DOM is on the other, and every time I want to access the DOM from the JavaScript engine, I have to pay that toll"
- John Hrvatin, Microsoft, MIX09, in this talk Building High Performance Web Applications and Sites at 29:38, also cited in the O'Reilly Javascript book by Nicholas C Zakas Pg 36, as well as mentioned in this post
Alerts
There are two places alert()
is called (one in huffman()
and one in the window.onerror
handler). This can be an issue because some users may have disabled alerts in a browser setting. It is better to use HTML5 <dialog>
element - it allows more control over the style and doesn't block the browser. Bear in mind that it isn't supported by IE and Safari but there is a polyfill
Overall feedback
It seems there are quite a few global variables referenced within various functions. This isn't totally bad but it makes things difficult - like unit testing. If functions accepted parameters and returned certain output then testing might be easier.
The answer by Ted Brownlow suggests using a Plain-old Javascript object (A.K.A. a POJO) to store the occurrences instead of an array - i.e. a mapping of characters to counts. This can eliminate the need to initialize the array and set all values to zero.
You may be interested to read other posts involving Huffman Encoding, including this one.
Suggestions
Initializing an array of zeroes
In the function count()
there is this code:
occurences = new Array(128); // Initialize with zero for (let i = 0; i < occurences.length; i++) { occurences[i] = 0; }
The loop can be avoided by using array.fill()
.
Excess variables
in the function isASCII
there is the variable test
that is returned immediately after being assigned. While this may be leftover from debugging the variable can be eliminated. The whole function could be expressed as a one-line arrow function.
Avoid excess DOM lookups
The code in huffman()
accesses DOM elements each time. While it may not be as much of an issue with todays browsers, it is wise to cache DOM references one they are available (e.g. in the DOMContentLoaded
event).
"...DOM access is actually pretty costly - I think of it like if I have a bridge - like two pieces of land with a toll bridge, and the JavaScript engine is on one side, and the DOM is on the other, and every time I want to access the DOM from the JavaScript engine, I have to pay that toll"
- John Hrvatin, Microsoft, MIX09, in this talk Building High Performance Web Applications and Sites at 29:38, also cited in the O'Reilly Javascript book by Nicholas C Zakas Pg 36, as well as mentioned in this post
Alerts
There are two places alert()
is called (one in huffman()
and one in the window.onerror
handler). This can be an issue because some users may have disabled alerts in a browser setting. It is better to use HTML5 <dialog>
element - it allows more control over the style and doesn't block the browser. Bear in mind that it isn't supported by IE and Safari (And seemingly Chrome on iOS) but there is a polyfill
Overall feedback
It seems there are quite a few global variables referenced within various functions. This isn't totally bad but it makes things difficult - like unit testing. If functions accepted parameters and returned certain output then testing might be easier.
The answer by Ted Brownlow suggests using a Plain-old Javascript object (A.K.A. a POJO) to store the occurrences instead of an array - i.e. a mapping of characters to counts. This can eliminate the need to initialize the array and set all values to zero.
You may be interested to read other posts involving Huffman Encoding, including this one.
Suggestions
Initializing an array of zeroes
In the function count()
there is this code:
occurences = new Array(128); // Initialize with zero for (let i = 0; i < occurences.length; i++) { occurences[i] = 0; }
The loop can be avoided by using array.fill()
.
Excess variables
in the function isASCII
there is the variable test
that is returned immediately after being assigned. While this may be leftover from debugging the variable can be eliminated. The whole function could be expressed as a one-line arrow function.
Avoid excess DOM lookups
The code in huffman()
accesses DOM elements each time. While it may not be as much of an issue with todays browsers, it is wise to cache DOM references one they are available (e.g. in the DOMContentLoaded
event).
"...DOM access is actually pretty costly - I think of it like if I have a bridge - like two pieces of land with a toll bridge, and the JavaScript engine is on one side, and the DOM is on the other, and every time I want to access the DOM from the JavaScript engine, I have to pay that toll"
- John Hrvatin, Microsoft, MIX09, in this talk Building High Performance Web Applications and Sites at 29:38, also cited in the O'Reilly Javascript book by Nicholas C Zakas Pg 36, as well as mentioned in this post
Alerts
There are two places alert()
is called (one in huffman()
and one in the window.onerror
handler). This can be an issue because some users may have disabled alerts in a browser setting. It is better to use HTML5 <dialog>
element - it allows more control over the style and doesn't block the browser. Bear in mind that it isn't supported by IE and Safari but there is a polyfill