Skip to content

Instantly share code, notes, and snippets.

@gmilby
gmilby / performanceTips2.js
Created July 19, 2013 22:53
more performance tips for jquery
1. Define local variables
When a variable is referenced, Javascript hunts it down by looping through the different members of the scope chain. This scope chain is the set of variables available within the current scope, and across all major browsers it has at least two items: a set of local variables and a set of global variables.
Simply put, the deeper the engine has to dig into this scope chain, the longer the operation will take. It first goes through the local variables starting with this, the function arguments, then any locally defined variables, and afterwards iterates through any global variables.
Since local variables are first in this chain, they’re always faster than globals. So anytime you use a global variable more than once you should redefine it locally, for instance change:
var blah = document.getElementById('myID'),
blah2 = document.getElementById('myID2');
to:
var doc = document,
blah = doc.getElementById('myID'),
blah2 = doc.getElementById('myID2');
@gmilby
gmilby / createUUID.js
Created September 18, 2013 16:46
angular - create unique id
angular
.module('uuidApp', ['lvl.services'])
.controller('uuidCtl', ['$scope', 'uuid', function($scope, uuid){
$scope.generateUuid = function() {
$scope.new = uuid.new();
$scope.nInfo = new Date();
};
$scope.showEmpty = function() {
$scope.empty = uuid.empty();
controllers.controller('MainCtrl', function($scope, $location, Facebook, $rootScope, $http, $location, Upload, Auth, User, Question, Category, Serie, Record, Location, Popup, Process, Card, Question) {
$scope.$on('authLoaded', function() {
$scope.isExpert($scope.main.serieId);
$scope.isMember($scope.main.serieId);
});
$scope.loadAuth = function() {
Auth.load().success(function(data) {
$scope.main.user = data.user;
$scope.$broadcast("authLoaded");
controllers.controller('MainCtrl', function($scope, $location, Facebook, $rootScope, $http, $location, Upload, Auth, User, Question, Category, Serie, Record, Location, Popup, Process, Card, Question) {
$scope.$on('authLoaded', function() {
$scope.isExpert($scope.main.serieId);
$scope.isMember($scope.main.serieId);
});
$scope.loadAuth = function() {
Auth.load().success(function(data) {
$scope.main.user = data.user;
$scope.$broadcast("authLoaded");
t1 = Thread.new do
i = 0
1_000_000.times do
i += 1
end
end
t2 = Thread.new do
j = 0
1_000_000.times do
j += 1
.directive('whenActive',
[
'$location',
($location)->
scope: true,
link: (scope, element, attr)->
scope.$on '$routeChangeSuccess',
() ->
loc = "#"+$location.path()
href = element.attr('href')
Calculate the distance between two coordinates
When we need to measure the distance between two points, we may use one of the following formulas: Haversine formula or Vincenty’s formula. There are two appropriately named functions:
01
function haversineGreatCircleDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000) {
02
// convert from degrees to radians
03
$latFrom = deg2rad($latitudeFrom);
<html>
<head>
<meta charset="utf-8">
<title>Clean-Markup Line Numbers™</title>
<style>
body {
width: 800px;
margin: 0 auto;
}
$http.post("http://example.appspot.com/rest/app", {"foo":"bar"})
.success(function(data, status, headers, config) {
$scope.data = data;
}).error(function(data, status, headers, config) {
$scope.status = status;
});
$http({
url: "http://example.appspot.com/rest/app",
method: "POST",
data: {"foo":"bar"}
}).success(function(data, status, headers, config) {
$scope.data = data;
}).error(function(data, status, headers, config) {
$scope.status = status;
});