Posts

[Javascript] Pass by Reference or Pass by Value?

Connection between variable, value and memory A variable is just a pointer to a location in memory. When you create a variable, your program allocates a space in memory, remembers it so that you can assign value to it and retrieve the value when you want. The 'reference' in 'pass-by-reference' refers to memory location. The 'value' in 'pass-by-value' refers to the actual value in the memory location. Why does this matter? When you use pass-by-value, it means that any argument you pass to a function only have their values copied and transferred to parameters inside the function definition. In other words, the parameters inside the function are not the same as the variables you passed in as arguments. When I say the 'same', even though their value is same, if you change one of them, the other will not change (if it is pass-by-reference, the other will change). Below is an example of pass-by-value: // A pseudo-code example of pass-by-va...

[ORM] Eager Loading and N+1 Query Problem

What is N+1 query problem? When your code loads the children in a parent-child relationship via the parent, most ORM have lazy-loading enabled by default, so queries are issued for the parent record, and then one query for EACH child record. As you can expect, doing N + 1 queries instead of a single query will floor your database with queries; something to avoid.  Consider this code: #Articles model class Article < ActiveRecord::Base     belongs_to :author end  #Authors model class Author < ActiveRecord::Base     has_many :posts end If you then ran: #In our controller @recent_articles = Article.order(published_at: :desc).limit(5) #in our view file @recent_articles.each do |article|     Title: <%= article.title %>     Author:<%= article.author.name %> # <-- this will trigger N+1 query end You would send 6 (5+1) queries to the database. 1 to fetch 5 recent articles, and then 5 for their correspo...

[Websites] Eager and Lazy Loading

[Website] Eager Loading, Lazy Loading A simple explanation is this: 1. Eager loading : you do everything when asked. Classic example is when you multiply two matrices. You do all the calculations. 2. Lazy loading : you only do a calculation when required. In the previous example, you don't do any calculations until you access an element of the result matrix. 3. Over-eager loading : this is where you try and anticipate what the user will ask for and preload it. Websites  In the context of a website, you can imagine that there are three ways the image loading could work on this page: ( eager );  Load only the displayed images on page load and load the others if/when they are required ( lazy ); and  Load only the displayed images on page load. After the page has loaded preload the other images in the background in case you need them ( over-eager ).  https://stackoverflow.com/questions/1299374/what-is-eager-loading

[Docker] Cli you cannot live without

docker ps  —  Lists running containers . Useful flags: -a / -all for all containers (default shows just running) —-quiet /-q to list just their ids (useful for when you want to get all the containers).  docker pull  — Most of your images will be created on top of a base image from the Docker Hub registry. Docker Hub contains many pre-built images that you can pull and try without needing to define and configure your own. To download a particular image , or set of images (i.e., a repository), use docker pull. docker build  — The docker build command builds Docker images from a Dockerfile and a “context”. A build’s context is the set of files located in the specified PATH or URL. Use the -t flag to label the image, for example docker build -t my_container . with the . at the end signalling to build using the currently directory. docker run  —  Run a docker container based on an image , you can follow this on with other commands, such as -it bash to then run bash from ...

[Prisma] Notes on Prisma + Docker

This is a documentation as a result of trying out https://github.com/prisma/prisma#quickstart Some notes on Prisma commands: docker-compose up -d : 'compose' starts the YAML file that configures your application's services. Can be used to run multiple containers at once. (YAML file: docker-compose.yml) docker-compose down: stops containers and removes containers, networks, volumes and images created by 'up' prisma deploy : prisma playground Some notes on postgres \l : show database \dt : show tables Accessing database (created by prisma) 1. docker ps -a (to get list of docker containers history) 2. Find the container with 'IMAGE' of postgres or mysql and remember its container id. 3. docker exec -it <container id> sh (attach onto docker container) 4. psql -d postgres -U prisma -W 5. Enter pw - usually 'prisma' 6. \dt default$default.  (to see all the tables in schema default$default) 7.  select * from default$default....

[Encoding] Binary to Text Encoding

Binary, ASCII and Text Data Binary data is a sequence of 8 bit bytes, where each byte can have a value between 0x00 and 0xFF. In general, we can’t assume much about this data, except that any byte could potentially have any value. ASCII data represent text as a sequence of bytes. In the ASCII system, byte values in the range 0x00 and 0x7F are used to represent English language letters (upper and lower case), numerals, punctuation symbols and various ‘control code’. Byte values above 0x80 have no well defined meaning in ASCII. Since ASCII data is not expected to contain byte values of 0x80 or greater, it is often called 7-bit data. Printable characters in ASCII are values in range 0x21 to 0x7E, which includes upper and lower case characters, numerals and all standard punctuations.   Text data is ASCII data which only contains printable and whitespace characters. Binary-to-text encoding is encoding of data in plain text. More precisely, it is an encodin...

[Encoding] Binary Mode

Have you ever tried writing a software to read a zip file and send it over the server, only for the server to receive something different? If you do, one of the possible reasons is you are not reading your zip file correctly. A beginner mistake is to read a zip file as if you are reading a text file.  Remember, text, in the computer world, are bytes interpreted by a specific type of encoding . If you program your software to read your zip file as if it is reading a text file, you might end up using the wrong encoding, and thus, be unable to interpret your zip file later on. Personal Experience I got this problem when I was trying to add a feature where you download zip from the server via my web site. My server and webapp were coded in Ruby, so I'm going to use Ruby syntax. At first, I used File.read(...) to read my zip file. Then I do an encode64 because my webapp crashes when it sees unreadable characters. In my webapp, I then decode64 (since I encode64-ed it in the se...