JS.Node Flashcards
What is a common error made when storing data as JSON ?
stringifying the data more than once before storage or not stringifying it at all.
what does array.filter() do?
filters an existing array and returns a new array that contains only the filtered values.
what does array.foreach() do?
Much like a for loop, it loops through each item in an array and performs a specified opperation on each item.
What does array.map() do?
Performs an opperation on each value within an array and returns a new array that contains the new mapped values.
What does array.reduce() do?
reduces the values in a given array to a single value.
What does it mean when we say that node uses “non-blocking I/O”?
It means that Node is asynchronous.
What are the two most common JSON commands?
JSON.stringify() and JSON.parse()
What does JSON.stringify do?
turns an object into a JSON object by adding double quotes to the keys and values in objects.
What does JSON.parse() do?
turns JSON objects into objects by removing the double quotes from the keys in an object.
Which module is used to work with files in node?
fs
Which module is used to provide colourful feedback in the console?
chalk
How does the V8 engine works
Node itself is written in C++ and the V8 engine is embedded into it. When writing javascript. The javascript and it’s c++ bindings are passed into the V8 engine which converts it to machine language which can be run on a machine like a computer or a server.
The javascript engine used by Chrome
V8
The javascript engine used by Edge
Chakra
The javascript engine used by Firefox
Spidermonkey
The javascript engine used by Edge
Javascriptcore
What is a javascript engine?
A program that compiles javascript down to machine language
What is the difference between an AOT and JIT compiler?
AOT (ahead of time) compilers, will compile the entire program into machine code ahead of time.
JIT (Just in time) compilers used a combination of interpretation and compilation and will compile blocks of code just before it is neede.
JIT is slower than AOT but it makes it easier to debug and maintain code.
The V8 engine is multi-threaded. What are the 4 main threads and what are there functions?
- Main thread: Fetch - compile -execute
- Compiler thread: Codegen(ignition): converts to
machine code Crankshaft(turbofan): optimizes hot
code - Profiler thread: Monitors code and finds “hot” sections
- Garbage collection thread: Garbage collection
How does crankshaft optimize code?
1. inlining: All the function in the "hot" path are replaced by the actual code from the function to improve performance 2. Hidden classes: The compiler generates hidden classes that maintains the pointers to every object property in a class structure so that it need not be looked up everytime 3. inline caching: A cache of the object type that is most likely to be passed to a function is maintained so that unnecessary lookups are avoided
what is a “method”?
Methods are functions defined as a propertie of an object or class. methods are executed using the dot notation.
Why are arrow functions not well suited for use as methods in an object?
Because arrow functions do not bind their own “this” value, instead they access the “this” value of the context they were created in. This can make it difficult to access properties within objects. Instead it would be better to use an anonymous function which binds its own “this” value
What are arrow functions best used for?
callback functions
What should we use in the place of array.filter() when looking for a single item in a large array? Why?
Array.find()
Array.filter() will look through each item in a list one at a time. It will continue to do so even if the item we are looking for has been found.
Example. In a list of 1000 items, if the item we are looking for is at index 89, it will continue to look through the remaining 911 items before stopping.
Array.find() will stop after it finds the first instance of the desired item.