• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
sirpalee

Javascript / HTML5 best practices

9 posts in this topic

Hi guys!

 

Recently I had some free time, and decided to dive into javascript and html5 stuff. I'm looking for some book / series of articles that are talking about how to write efficient code, or what are the best practices, especially centered around html5 and game development.

 

I looked around the web, found a huge amount of resources, but it's really hard to decide where to begin. I have several years of experience with c++, so learning the syntax is simple, so I know how to organize my code or do anything I want. But I understand, that developing for browsers, or working with script languages like JS is different. I just don't want to follow some random tutorial that pops up in my google search and learn bad practices.

 

So, in short do you know any good javascript / html5 resources about game development?

 

Cheers,

Pal.

1

Share this post


Link to post
Share on other sites

Honestly, so few people know what they're actually talking about with JS, it's really hard to make any recommendations. There's a tendency these days to treat JS as a "low level" language (lol, I know) and everyone is scrambling to figure out what the best other language to get that "compiles" to JS (lloll, I double know).

 

As long as you're following very basic principles of OO and Functional Programming, not writing sloppy code, you're going to be fine. There are some cross-browser issues you will have to watch out for, but you'll find them with experience and figure out how to get around them. JS isn't hard, just spend a little time with it and you'll know what to do.

1

Share this post


Link to post
Share on other sites
There are some cross-browser issues you will have to watch out for, but you'll find them with experience and figure out how to get around them.

Well isn't that what JQuery, Mootools, and other JS libraries are for?
0

Share this post


Link to post
Share on other sites

First and foremost, since JavaScript is a programming language, the same general rules apply to writing efficient code:

  • Use the best algorithm for the task. Example, don't spend hours optimizing your bubble sort procedure when a quicksort will always be faster.
  • Use the features provided by the language's standard library, because they're probably written better than anything you'll create. Example, don't write your own quicksort. The standard library sort probably already does it.

Since javascript is a dynamic language, most of the same rules apply when optimizing:

  • Function calls to JavaScript are expensive. Calls to built-in functions may or may not be.
  • Dictionary references are expensive. Example: dictionary['thing']. If you need to access the same element more than once in a loop, assign it to a local variable before the loop then reference it with the variable.
  • Consider dictionary.thing to be equivalent to dictionary['thing'] as far as performance is concerned.
  • Functions are first class objects, and being first class objects they are no different than any other element in a dictionary on your objects: This line: "myobject.function()" can be considered the equivalent of "myobject['function']()" as far as efficiency goes. You can hoist this outside the loop too: 
    var myobject = new MyObject(whatever);
    var hoisted = myobject.function;
    
    for (var i = 0; i < 1000; ++i) {
        for (var j = 0; j < 1000; ++j) {
            hoisted(i, j);
        }
    }

     

     

  • "for (var i = 0; i < arrayLength; ++i)" is more efficient than "for (var i in array)" or even "for (var i = 0; i < array.length; ++i)" when the dictionary can be indexed sequentially.
  • Use built-in functions whenever possible, because they're often implemented in a low level language and compiled to machine code.
  • jsPerf is a great resource.
  • Don't optimize until you've profiled.
  • What is fast in Chrome might be slow in Safari, Firefox or Internet Explorer.
Edited by smr
1

Share this post


Link to post
Share on other sites
There are some cross-browser issues you will have to watch out for, but you'll find them with experience and figure out how to get around them.

Well isn't that what JQuery, Mootools, and other JS libraries are for?

I haven't used Mootools that much, but I have used JQuery a fair bit. I like the design of the interface a lot. Unfortunately, I think it adds a lot of overhead, especially on mobile devices. I don't think there are *that* many cross-browser issues to warrant such a huge cost in download size and startup time. Yes, the issues are annoying, but they are really easy to get around, and then you've learned it and never have to deal with it again.

1

Share this post


Link to post
Share on other sites

First and foremost, since JavaScript is a programming language, the same general rules apply to writing efficient code:

  • Use the best algorithm for the task. Example, don't spend hours optimizing your bubble sort procedure when a quicksort will always be faster.

  • Use the features provided by the language's standard library, because they're probably written better than anything you'll create. Example, don't write your own quicksort. The standard library sort probably already does it.

Since javascript is a dynamic language, most of the same rules apply when optimizing:

  • Function calls to JavaScript are expensive. Calls to built-in functions may or may not be.

  • Dictionary references are expensive. Example: dictionary['thing']. If you need to access the same element more than once in a loop, assign it to a local variable before the loop then reference it with the variable.

  • Consider dictionary.thing to be equivalent to dictionary['thing'] as far as performance is concerned.

  • Functions are first class objects, and being first class objects they are no different than any other element in a dictionary on your objects: This line: "myobject.function()" can be considered the equivalent of "myobject['function']()" as far as efficiency goes. You can hoist this outside the loop too: 
    var myobject = new MyObject(whatever);
    var hoisted = myobject.function;
    
    for (var i = 0; i < 1000; ++i) {
        for (var j = 0; j < 1000; ++j) {
            hoisted(i, j);
        }
    }

     

     


  • "for (var i = 0; i < arrayLength; ++i)" is more efficient than "for (var i in array)" or even "for (var i = 0; i < array.length; ++i)" when the dictionary can be indexed sequentially.

  • Use built-in functions whenever possible, because they're often implemented in a low level language and compiled to machine code.

  • jsPerf is a great resource.

  • Don't optimize until you've profiled.

  • What is fast in Chrome might be slow in Safari, Firefox or Internet Explorer.

 

This is all very good advice, with the exception of "quicksort is always faster than bubblesort". That isn't necessarily true. Bubblesort is very fast on nearly sorted data and quicksort has a large constant value to its runtime that makes some algorithms like insertionsort faster than it for small sets of data. It really, really depends on the situation, which I think was your more general point, assume nothing.

 

Really, really, assume nothing. There is so much out there that can go wrong or can change at the last minute or looks one way but is really another. Whenever you find yourself in a situation of not understanding why something is broken, that is a big hint that you have a false assumption somewhere and need to start proving and disproving them.

1

Share this post


Link to post
Share on other sites

My general point was that the first and most important rule to writing efficient code is choosing the correct algorithm for the problem. I was generalizing in the case of quicksort vs bubble sort as you correctly assumed.

1

Share this post


Link to post
Share on other sites

Hi everyone!

 

Thank you for the tips, and links they are really useful. It seems that I have to think a bit differently, than with c++ :) .

 

Cheers,

Pal.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0