Skip to main content

Posts

Showing posts from May, 2014

Reverse an Array while considering Memory Allocation

The Problem: At the company I work for, we interview for front-end developer positions quite frequently. One of my coworkers always asked the interviewee how to reverse an array in JavaScript.  Just for fun, I decided to to take on the challenge since .net makes it too easy (array.Reverse()). Originally, my answer to the question was this: var stringArray = [" Joey!", " Is", " Name", "my", " Hello,"]; var temp = new Array(); for(i = 0; i <  stringArray .length; i++) temp.push(actual[( stringArray . length - 1) - i]); console.log(temp); My coworker said my solution would definitely work, but he then asked how I can improve the answer by saving on memory.  What I learned: I didn't know how to improve the answer until I learned about JavaScript's  splice  method.  Here's my improved script using splice : var stringArray = [" Joey!", " Is", " Name", "my"...

Static Classes can be Shared by Multiple Threads!

My Mistake: I created a static class for a particular project with the sole purpose of storing session data (user full name, user name).  I could have stored the data in a session object, but I guess I wanted to try something different.  When I deployed the project, I noticed that when multiple users logged into the site, the display name would be inconsistent.  I couldn't figure out the reason for this until I had a discussion with a coworker (thanks Clark) What I Learned: A static class scope lasts as long as a process is running.   Many threads can share an instance, so whenever a user would log in, the existing data would be overwritten by the person that just logged in .  The user that logged in before the latest person that logged in would show a different name than their own.  I should probably use static classes for development tools (like a utility class), not for storing session data.  Good thing I didn't show a...