ARTS-Week Four
Algorithm
Problem
3Sum
Given an array nums
of n integers, are there elements a, b, c in nums
such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
The solution set must not contain duplicate triplets.
Solution
Review
Artical
Understanding Higher-Order Functions in JavaScript
Link
https://blog.bitsrc.io/understanding-higher-order-functions-in-javascript-75461803bad
Review
Functions are the first-class citizens in JavaScript. In JavaScript, functions are special type of objects. We can add properties to functions, assign functions to variables, pass functions as parameters.
A Higher-order function is a function that may receive a function as an argument and can even return a function.
Tips
If you want to chage the value of an array element., you can do it like this:
Share
Artical
How JavaScript works: inside the V8 engine + 5 tips on how to write optimized code
Link
Summary
How to write optimized JavaScript
Order of object properties: always instantiate your object properties in the same order so that hidden classes, and subsequently optimized code, can be shared.
Dynamic properties: adding properties to an object after instantiation will force a hidden class change and slow down any methods that were optimized for the previous hidden class. Instead, assign all of an object’s properties in its constructor.
Methods: code that executes the same method repeatedly will run faster than code that executes many different methods only once (due to inline caching).
Arrays: avoid sparse arrays where keys are not incremental numbers. Sparse arrays which don’t have every element inside them are a hash table. Elements in such arrays are more expensive to access. Also, try to avoid pre-allocating large arrays. It’s better to grow as you go. Finally, don’t delete elements in arrays. It makes the keys sparse.
Tagged values: V8 represents objects and numbers with 32 bits. It uses a bit to know if it is an object (flag = 1) or an integer (flag = 0) called SMI (SMall Integer) because of its 31 bits. Then, if a numeric value is bigger than 31 bits, V8 will box the number, turning it into a double and creating a new object to put the number inside. Try to use 31 bit signed numbers whenever possible to avoid the expensive boxing operation into a JS object.
评论