Skip to content

Commit 5fc1ac4

Browse files
improve performance
- hoist variables to top - cache array.length - cache document object - reduce filesize
1 parent b2cc1ae commit 5fc1ac4

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

loudlinks-1.0.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
var loudlinks = (function(){
2-
1+
var loudlinks = (function(document) {
2+
33
// Create audio element and make it awesome
44
var generateAudioElement = (function() {
55
var audioPlayer = document.createElement('audio'); // create the audio element
6+
var source1 = document.createElement('source'); // creating a source element
7+
var source2 = document.createElement('source');
8+
69
audioPlayer.id = 'loudLinksAudioPlayer'; // give the audio element the proper id
710
audioPlayer.preload = true; // audio element preload attribute
811

9-
var source1 = document.createElement('source'); // creating a source element
10-
var source2 = document.createElement('source');
1112
source1.id = 'mp3Source';
1213
source2.id = 'oggSource';
1314

@@ -42,6 +43,7 @@ var loudlinks = (function(){
4243
var audioSrc = element.getAttribute('data-src'); // getting the sound name from the data-src Attribute
4344
var soundMp3Link = mp3Location + audioSrc + '.mp3'; // settnig the mp3 sound in a variable
4445
var soundOggLink = oggLocation + audioSrc + '.ogg'; // settnig the ogg sound in a variable
46+
4547
mp3Source.setAttribute('src', soundMp3Link); // putting the mp3 sound link in the src Attribute of <source>
4648
oggSource.setAttribute('src', soundOggLink); // putting the mp3 sound link in the src Attribute of <source>
4749

@@ -56,6 +58,7 @@ var loudlinks = (function(){
5658
// checking if the data-src Attribute isn't empty
5759
checkAttribute = function(element) {
5860
var audioSrc = element.getAttribute('data-src'); // getting the sound name from the data-src Attribute
61+
5962
if (audioSrc) {
6063
return true;
6164
} else {
@@ -106,20 +109,25 @@ var loudlinks = (function(){
106109

107110
// Go crazy! Scan all the links and see if they have the 'data-src' Attribute and do the events
108111
runTrackers = function() {
109-
for (var i = 0; i < LoudLinksHover.length; i++) { // Hover
112+
var i;
113+
var loudLinksHoverLength = LoudLinksHover.length;
114+
var loudLinksClickLength = LoudLinksClick.length;
115+
116+
for (i = 0; i < loudLinksHoverLength; i++) { // Hover
110117
trackHover(LoudLinksHover[i]);
111118
}
112-
for (var i = 0; i < LoudLinksClick.length; i++) { // Click
119+
120+
for (i = 0; i < loudLinksClickLength; i++) { // Click
113121
trackClick(LoudLinksClick[i]);
114122
}
115123
}
116124

117125
// Check if the browser supports audio then get crazy!
118-
if (checkAudioSupport()){
126+
if (checkAudioSupport()) {
119127
console.log('Audio works like a charm 👍');
120128
runTrackers();
121129
} else {
122130
console.log('Oh man 😩! \nI\'m sorry but your browsers doesn\'t support awesomeness.')
123131
}
124132

125-
})();
133+
})(document);

loudlinks-1.0.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)