So you can add your html to your document by appending it.
$game.append('<div data-letter="A">A</div>');
or you could:
var game = document.getElementById('game');
var div = document.createElement('div');
$(div).data('letter', 'A');
game.appendChild(div);
My question is which of these methods is preferable.
I am using data attributes for speed and currently I am using the former example (.append()). It works fine.
However when I dynamically change the data for this element like such:
$(this).data('letter', 'U');
it changes the data stored in memory for the element, but it does not update the attribute. The attribute in this example would stay as 'data-letter="A"'. Again everything works great behind the scenes and when I call $(this).data('letter') I get the letter 'U'. But it got me questioning whether I should avoid data attributes. Are the attributes read once when they are appended, then uploaded into memory and the attributes are than left for dead. Or are the attributes read when no .data() is found?
I am concerned whether it is the latter because that would not provide optimal speed performance. I want to make sure everything is uploaded into memory when I append the element.