local storage and performance Flashcards

1
Q

How are cookies set and stored?

A

The server sends the cookie in the HTTP server response header as a name = value pair. The browser stores the cookie and sends it back in the request header in the same way.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a way to get around the limitation on number of cookes that a domain can send?

A

Use subcookies. Rather than name = value, use name = name1=value1&name2=value2…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you write a try/catch statement?

A
try { 
  if ( a ) { throw errorObject };
} catch (error) { // error arg is required
  doSomething( );
} finally { // optional }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

localStorage vs. IndexedDB

A

localStorage is synchronous and string only storage, intended mainly for smaller amounts of data. It will block page loading.
IndexedDB is more database-like; asynchronous, for greater amounts of data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How is an object represented in json (4 key elements) ?

A

Like a javascript object, but with a few key differences: key must be in quotes; no comma after last pair; no trailing semicolon; must use double quotes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When is try-catch statement most useful?

A

For handling an error that is out of your control —for example, when dealing with a library whose code you can’t touch.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When should you catch errors vs. when should you throw errors using throw new Error( )?

A

Catch errors only if you know what to do — you want to prevent browser from doing its default thing.
Throw errors if you don’t know what to do. Purpose is to provide information about why error occurred.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the most bomber way to do type detection (e.g. to determine if a value is an array or function or regexp or whatever)?

A

Object.prototype.toString.call ( valueToTest );

This will return a string in format of “[Object NativeConstructorName “]; // e.g. [Object Array]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

JSON allows representation of what types of values?

A

Simple values, objects (string-value mappings), and arrays. It doesn’t more complex data types like functions or regular expressions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the JSON object’s methods? And what do they do?

A

JSON.stringify(someObj) serializes an object into a JSON string.
JSON.parse(someJSON) turns a JSON string into a javascript value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What arguments does JSON.stringify() take?

A

The object to serialize; a filter, which can be an array or a function; indentation amount.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the execution context in javascript?

It can be thought of as an object with three properties – what are they?

A

An execution context is created every time a function is called (there’s also one for evals, withs, and global). 1. variable object; 2. scope chain; 3. ‘this’ object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can you filter what data gets serialized into a JSON string?

A

The second stringify argument can be an array of keys (e.g. [“title”, “author”]). Only those keys will be serialized.
Or it can be a function that can be more nuanced or allow you to make changes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is ECMA5 definition of a property?

A

An association between a name and a value that is part of an object
In other words, part of an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is ECMA5 definition of a variable?

A

There isn’t one. But it seems to be something like this:

An association between a name and a value that is part of an execution context. It belongs to a VariableObject.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why are properties and variables sometimes interchangeable? E.g.var foo = “bar”; log(window.foo); and window.bar = “foo”; log(bar).

A

This only works because the global object (parent of properties) and the global VariableObject (parent of variables) happen to be the same. In the function context, of course, property/variable switching will fail.

17
Q

Code can be executed within what types of execution context?

A

global (only one); functional (any time a function is called); eval;

18
Q

What is the activation object / variable object?

A

Every execution context has an activation object, which contains all the variables and functions that are in its scope, plus the arguments object, plus the args. For functions, the activation object is called a variable object.

19
Q

Why is it important to avoid global lookups?

A

Because global variables and functions involve a scope chain lookup, which is expensive. example : cache reference as in, var doc = document;

20
Q

What is a multiple property lookup?

A
window.location.href.substring(window.location.href.indexOf(“?”));
Expensive. If used more than once, store in a local variable. var href = window.location.href;
21
Q

What are four steps to optimizing a loop?

A

Decrement iterators are more efficient. (start at max and go to 0);
Since it must be evaluated every iteration, simplify terminal condition as much as possible. Avoid property lookups.
Simplify loop body.
Use posttest loops (do/while) because they avoid the initial evaluation.

22
Q

It’s more performant to have fewer statements. What are some ways to reduce the number of statements?

A

Declare a bunch of variables at once with commas;
Make an object or array using literals rather than multiple statements.
Combine iterative values into statement ( e.g. name = values[ i++ ] )