DN-02: Frontend Notes
Drive-by frontend notes, part two.
published Apr 03, 2017 tags
#javascript
#frontend
#notes
~/posts/dn-02 $ cat post.md
DOM operations
- Creating nodes
createDocumentFragment()— a DOM fragment.createElement()— a specific element.createTextNode()— a text node.
- Add, remove, replace, insert
appendChild()removeChild()replaceChild()insertBefore()— insert before an existing child.
- Lookup
getElementsByTagName()— by tag name.getElementsByName()— bynameattribute. IE is forgiving and also returns elements whoseidmatches the name.getElementById()— by id; unique.
iframe pros and cons
Pros:
- Loads slow third-party content (icons, ads) without blocking.
- Security sandbox.
- Loads scripts in parallel.
Cons:
- iframe blocks the parent page’s
onloadevent. - An empty iframe still takes time to load.
- No semantics.
Optimizing assets
- Concatenate files.
- Minify and compress.
- Host on a CDN.
- Cache (multiple domains for cacheable assets).
- Other.
Things that cause memory leaks
- A leak means an object stays around after it’s no longer needed.
- The GC periodically scans objects and counts references. An object with refcount 0, or one whose only references are cyclic, can be collected.
- Passing a string instead of a function as the first argument to
setTimeoutleaks. - Closures, console logs, cyclic references between two objects that keep each other alive.
What happens from URL entry to a rendered page
- The browser opens a thread to handle the request and starts a DNS lookup against a remote DNS server to get the IP.
- Browser and server perform a TCP three-way handshake: SYN, SYN-ACK, ACK.
- Once the TCP connection is up, the browser sends an HTTP GET on the connection. The server finds the resource and responds with HTTP; status 200 means success.
- The server serves the resource and the client begins to download it.
Ways to create a JS object
- Factory pattern
- Constructor pattern
- Prototype pattern
- Constructor + prototype hybrid
- Dynamic prototype
- Parasitic constructor
- Durable constructor
Six ways to do JS inheritance
- Prototype chain
- Constructor stealing
- Combined (prototype + constructor stealing)
- Prototypal
- Parasitic
- Parasitic combined
Same-origin policy
“Same origin” means same protocol, same domain, and same port. The same-origin policy is a security boundary — a script can only read properties of windows and documents from the same origin.
Non-blocking JS loading
- Put scripts at the bottom of the body.
- Group scripts: every
<script>tag blocks parsing while it downloads, so fewer tags helps. Applies to inline and external scripts alike. - Non-blocking: load JS after the page is done, i.e. after
window.onload.defer: IE4+ and Firefox 3.5+.- Dynamic script elements — the DOM lets you build the page in JS, including its own scripts:
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "file.js";
document.getElementsByTagName("head")[0].appendChild(script);
What is eval
- Parses a string as JS and runs it.
- Should be avoided: unsafe, and slow (two passes — parse, then execute).