JavaScript loop and object iteration (optimization)

Reading Time: 3 minutes

Nowadays, it’s interesting that loops become part of our life as developers and we use them at least one time a day. Because of that, one day I decided to investigate and go deeper into JavaScript loops, where I found very interesting things and if I do not share them with you, I am going to feel guilty.

Before you continue reading, I would strongly recommend you to read my previous blog which I believe you will find very useful to create a full picture of the loops. So, go on and read it.

Object properties iteration

Let’s first analyze object iteration and suppose that we have an object, something like:

var obj = {
    property1: 1,
    property2: 2,
    …
}

First, what comes to our mind is to iterate them with the standard for each iteration:

for (var prop in obj) {
    console.log(prop);
}

In this case, we are going to iterate through the object properties but is it the correct way? The answer is yes and no, depends on your needs. Another way to iterate trough is to exclude all inherited properties, which in some case we do not need. So, we can exclude them by using the JavaScript method hasOwnProperty(). You can find an explanation about in operator and hasOwnProperty() in my previous blog.

Since we learned some object optimization/improvements/usage, now the question is, can we really do an optimization?
The answer is yes. Before I am going to show you how we can do that, let’s spend some time on the loops.

Loop iteration

In order to continue the previous example, I will continue explaining the loops with object iteration (of course you can test it with a list of integers like the speed test examples or anything you want).

To accomplish that, we will need the JavaScript method Object.keys().

  • Object.keys() method returns an array of a given object but only the own properties of the object

Let’s write the standard for loop:

var keys = Object.keys(obj)
for (var i = 0; i < keys.length; i++) {
    console.log(obj[keys[i]]);
}

Now we have a solution where we decreased the iteration time by eliminating the time for the evaluation `keys.length` from O(N) to O(1) which is a big time saving if we iterate big arrays.
So, during the development, if you are not limited (like applying some best practices,…), you can add another optimization, by using while loop.

var i = keys.length;
while (i--) {
    console.log(obj[keys[i]]);
}

In this case, we do not declare a new variable, we don’t execute new operations and the while loop will automatically stop when it reaches -1.

Speed testing:

Since the new browsers like Chrome are very fast and optimized, in order to see the best speed differences, I would suggest executing the loops on IE where you will be able to see a real speed difference between the loops.

var arr = new Array(10000);

Example speed test 1	
console.time();                        
for (var i = 0; i < arr.length; i++) {
    // operations...		
    var sum = i * i;   		
}
Execution 1: 4.4ms		
Execution 2: 5.5ms		
Execution 3: 5ms			
Execution 4: 4.6ms			
Execution 5: 5ms	
					
Example speed test 2                    
console.timeEnd(); 	          
while (i--) {
    // operations...
    var sum = i * i;
}
console.timeEnd();

Execution 1: 3.7ms
Execution 2: 4.8ms
Execution 3: 3.9ms
Execution 4: 3.8ms
Execution 5: 4.2ms

Thank you for reading and I would appreciate any feedback.

Making Swift networking code more readable

Reading Time: 3 minutes

With Swift 5 a new type got introduced:

@frozen public enum Result<Success, Failure> where Failure : Error {

    /// A success, storing a `Success` value.
    case success(Success)

    /// A failure, storing a `Failure` value.
    case failure(Failure)

The Result type is an enum consisting of 2 cases. The success and the failure case. Each of them can hold a generic value. The failure case, however, is limited to Types extending the Error type.

Not a big deal? Sure, but it’s the little things which add up and make a difference in the long run.

Lately, I was migrating from SwiftyJSON to native JSON parsing. Each network call was implemented in the following way:

func fetchSomething(completion: @escaping (SomeReturnValue?, SomeError?) -> Void) {
    NetworkingTool.request { (response) in
        guard response.isValid
            else { completion(nil, .somethingBad); return }
        do {
            let returnValue = try SomeReturnValue(response: response)
            completion(returnValue, nil)
        } catch {
            completion(nil, .scarry)
        }
    }
}

Looks okayish. Good. So let’s use it:

fetchSomething { (result, error) in
    guard error == nil
        else { handleError(error: error); return }
    doSomething(result: result)
}

Ok. But how to implement the doSomething? With an optional? This can’t be right, right? Force unwrap the result? And what about the error case? Force unwrap it? Oh and wait, what about the case where neither a result nor an error is returned? Is this even a thing? Ok, let me look up the implementation…

So a tiny bit of ambiguity paired with different people working on different parts of the network stack for different features can cause a real heterogeneous system. (Which does not imply that this is a bad system!)

If the company you’re working for is in favour of code ownership, you may not encounter this one. But so far no company I worked for was about code ownership. It’s usually your code is my code is our code, comrade. Period. There are simply too many trucks outside.

As long as code ownership isn’t a thing and you do not want to spend time on endless syntax and architectural discussions with little benefit or enforce a (new) best practice on all of your colleagues. Again. It comes really handy to have a built-in Result type which is reasonably unambiguous.

And since we all know that we’re spending more time reading code than writing, this saves us all valuable time.

New to programming? 5 things you should pay more attention to

Reading Time: 5 minutes

You decided to start learning programming. You have started to learn programming concepts, you have decided which language you want to learn, and everything looks great.

Except it isn’t.

It’s frustrating; it’s boring; it’s painful. I am not here to make your life easy, but I hope that I will make it a little easier. Here are the 5 things that I believe will help you to become a better programmer.

Find the right source to learn from

I had a professor who said:

“It’s better to spend more time researching where to learn from, than actually learning from one source.”

And this is gold.

Let’s say that you have found a great book or a great video course that everyone is loving. You think that you will love it too, every word that you read/hear in the book/course you will understand, and after you finish it, you will become a master of the things you will learn (at least, I thought like that).

And maybe you will, but probably you won’t. Most (or let’s say, some) of the things you won’t understand, and it’s natural. You will try to read/watch again and again, but it’s not getting any clearer.

My advice is, try to find a great book/course, and start learning from, but use it more as a reference than learning source.

I am not suggesting to only go through the content. Try to understand the concept, but also research it (on Google). Look for more resources, more explanation, more examples. When you will understand the concept, save the source that helped you the most (bookmark the page), and search for examples that you can solve.

This way, it is easier to learn, because you are combining the explanations of different sources, and you are sticking with the most simple explanation that is working for you. Also, research is more interesting than reading\listening the same thing all over again.

Understand the base (minimum) necessary logic rather than implementation

This is important for a few reasons:

  • First, if you understand the logic, it will be easier to learn the implementation
  • Second, the implementation may change, but the base necessary logic won’t

At the very beginning, it will be difficult to differentiate between logic and implementation, and maybe you should try to learn and remember everything, but later try to understand and study just the minimum necessary required things.

I still google some basic things. But because I know what I have to do, I exactly know what to search for (only the implementation/syntax).

With this approach, you will spend your time wisely, and you will be able to learn more important things.

To learn your first programming language is very hard, but that’s because you have to learn programming concepts (the logic). After you learn that, you can learn any language (the implementation) you want, in a matter of weeks.

Code, code, code…

Learning programming is like learning how to drive, except it’s safer (at least, physically). You can read, you can learn, but when you sit down and start to drive, you’ll realize that you haven’t learned anything.

That’s why you should focus on coding. When you study something, try to learn the minimum, so you can start to code, and then code as you learn. There is a great answer on Quora, that mentions 3 rules that you should follow when coding.

  • Write at least one line of code per day
  • First, write code, then refactor
  • No distractions when coding

Here, you can check the answer, that has reasoning for these rules. Maybe you can forget the second rule, but the other 2 are very important.

Attitude

I had to mention attitude. It is a hard path, especially at the beginning, so the right attitude is required. Hard work, believing in yourself, learning to say YES to everything is needed. More precisely, you say NO only when you are 100% sure that it isn’t possible to do. In any other case, you say YES, and you investigate, you try different approaches, you ask for help if it is necessary, you do everything you can. A time will come when you will need to learn to say NO, but first, you have to learn to say YES.

Rest

Of course, don’t forget to rest. You have to rest from the hard work you have done. Most of the stupid things I have done were when I was too tired. When you are tired, you don’t think rationally. You just want to finish your task, no matter. That’s when the biggest mistakes come. You won’t learn anything, you won’t do anything well, you are just wasting your time and nerves.