City search

Search for cities in a JSON array. String search is faster and make the same result.

by Zmetser

HTML

<div id="l_cities_json" style="display: none">
        [{"id": 417, "name": "Budapest", "country": {"country_code": "hu", "country_name": "Magyarország"} },{"id": 608, "name": "Debrecen", "country": {"country_code": "hu", "country_name": "Magyarország"} },{"id": 1868, "name": "Miskolc", "country": {"country_code": "hu", "country_name": "Magyarország"} },{"id": 2744, "name": "Szeged", "country": {"country_code": "hu", "country_name": "Magyarország"} },{"id": 2289, "name": "Pécs", "country": {"country_code": "hu", "country_name": "Magyarország"} },{"id": 1003, "name": "Győr", "country": {"country_code": "hu", "country_name": "Magyarország"} },{"id": 2104, "name": "Nyíregyháza", "country": {"country_code": "hu", "country_name": "Magyarország"} },{"id": 1340, "name": "Kecskemét", "country": {"country_code": "hu", "country_name": "Magyarország"} },{"id": 2758, "name": "Székesfehérvár", "country": {"country_code": "hu", "country_name": "Magyarország"} },{"id": 2854, "name": "Szombathely", "country": {"country_code": "hu", "country_name": "Magyarország"} },{"id": 2933, "name": "Tatabánya", "country": {"country_code": "hu", "country_name": "Magyarország"} },{"id": 270, "name": "Békéscsaba", "country": {"country_code": "hu", "country_name": "Magyarország"} },{"id": 765, "name": "Érd", "country": {"country_code": "hu", "country_name": "Magyarország"} },{"id": 3255, "name": "Veszprém", "country": {"country_code": "hu", "country_name": "Magyarország"} },{"id": 3311, "name": "Zalaegerszeg", "country": {"country_code": "hu", "country_name": "Magyarország"} },{"id": 2644, "name": "Sopron", "country": {"country_code": "hu", "country_name": "Magyarország"} },{"id": 721, "name": "Eger", "country": {"country_code": "hu", "country_name": "Magyarország"} },{"id": 1962, "name": "Nagykanizsa", "country": {"country_code": "hu", "country_name": "Magyarország"} },{"id": 702, "name": "Dunaújváros", "country": {"country_code": "hu", "country_name": "Magyarország"} },{"id": 1138, "name":...

JavaScript

/*jslint browser: true, maxerr: 50, indent: 4 */
/*global $*/

(function() {
    "use strict";

    var print, profiler;

    // Helper functions
    Object.prototype.equals = function(a) {
        var b;
        for (b in this) {
            if (typeof(a[b]) == "undefined") {
                return false
            }
        }
        for (b in this) {
            if (this[b]) {
                switch (typeof(this[b])) {
                case "object":
                    if (!this[b].equals(a[b])) {
                        return false
                    }
                    break;
                case "function":
                    if (typeof(a[b]) == "undefined" || (b != "equals" && this[b].toString() != a[b].toString())) {
                        return false
                    }
                    break;
                default:
                    if (this[b] != a[b]) {
                        return false
                    }
                }
            } else {
                if (a[b]) {
                    return false
                }
            }
        }
        for (b in a) {
            if (typeof(this[b]) == "undefined") {
                return false
            }
        }
        return true
    };

    print = function(text) {
        $("body").append(text);
    };

    profiler = function(callback, times) {
        var time, ret, start, duration;

        time = function() {
            return (new Date()).getTime();
        };

        start = time();

        for (var i = 0; i < times; i++) {
            ret = callback();
        }

        duration = time() - start;

        print("<pre>" + callback.toString() + "</pre><p>Took " + duration + " miliseconds. (called " + times + " times)</p>");

        return ret;
    }

    // Start the actual test functions
    var query = "Budape",
        call  = 100;

    // Notice, the String.toLowerCase method here!
    // String.indexOf is case sensitive, regex may not. And conversion is...