Django (SQL/”Model”) Performance Optimization

Django makes it easy to write code, which performs bad, since it is automatically executing SQL statements when you are handling models. If you don’t know about the details behind that, you can easily end up executing thousands of queries.

def get_books_by_author():
    books = Book.objects.all()
    result = defaultdict(list)    
    for book in books:
        author = book.author
        title_and_author = '{} by {}'.format(
            book.title,
            author.name
        )
        result[book.library_id].append(title_and_author)    return result

This will execute for each book an author query! Which means 1 query for books and n queries for n books!

books = Book.objects.all().select_related('author')

This will add “author” into the Book.objects.all() query and you just execute one query!

However, select_related only works for one-to-many and one-to-one. Many to one relations can be optimized with prefetch_related().

Continue reading “Django (SQL/”Model”) Performance Optimization”

Async: how it is handled internally in JS (Tasks, Microtasks/Jobs, Queues, Schedule)

I have found this wonderful step-by-step-diagramms in an article from I want to share with you: https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/ It also contains an example with event bubbling 🙂

In summary:

  • Tasks execute in order, and the browser may render between them
  • Microtasks execute in order, and are executed:
    • after every callback, as long as no other JavaScript is mid-execution
    • at the end of each task

What programming language should I learn?

Well, you might have noticed, I am an excessive medium reader. This time I stumbled across an article about, what programming languages should I learn (as a beginner). Whereas most suggestions are either Python or JavaScript, Eric Girouard suggests the one and only fundamental thing in computer science: Logic! If you are able to solve problems logically fitting to a computer’s “mind”, it doesn’t matter which programming language you are using to express it.

Closures (in JavaScript)

If you return a function (instead of a value or object), you will have to think about closures. You are not just returning the function definition but also the all the variables in scope at the time of creation of the function! This is called a closure. A read a nice Medium article with some very detailed step-to-step explanation, I’d like to recommend.

Continue reading “Closures (in JavaScript)”