Tuesday July 21, 2009 | |
Update: Added more data to show differences.
During the journey of finding a better linear array search function in javascript, I decided to write my own. I decided to take a different approach. Here’s the outcome.
Array.prototype.small_search = function(search) {
if (this[0] === search){ return 0; }
var s = '\x00';
var a = this.join(s);
var b = a.search(search)-1;
return a.substring(0, b).split(s).length;
};
With an array of less than 150 elements, it is much faster than Dustin Diaz’s in_array function. While they aren’t the same, his can be adapted to return the index also.
The only downside with my function is that it becomes much slower after the array length is larger than 150 elements. So, if you have a small array, go ahead and use this, otherwise use Dustin’s.
Times
Array Length: 5
small_search: 0.020ms
in_array: 0.098ms
Array Length: 10
small_search: 0.024ms
in_array: 0.084ms
Array Length: 50
small_search: 0.042ms
in_array: 0.095ms
Array Length: 100
small_search: 0.069ms
in_array: 0.106ms
Array Length: 150
small_search: 0.087ms
in_array: 0.111ms
Array Length: 300
small_search: 0.203ms
in_array: 0.137ms
Array Length: 500
small_search: 0.418ms
in_array: 0.132ms
Array Length: 1000
small_search: 0.813ms
in_array: 0.138ms
Array Length: 5000
small_search: 3.530ms
in_array: 0.312ms
Array Length: 10000
small_search: 6.718ms
in_array: 0.441ms
Test it out on your own and come back with results.
Let’s say you have a huge text file with couple million lines. Now you want to split it up into 100, 200, or 300 line files. Bash makes it pretty easy with split.
In Bash:
split -l 500 file.txt
That splits up the file file.txt into 500 line chunks. What is produced are files such as xaa, xab, xac and so on.
Be sure to do man split for more options.
Sometimes it’s very useful to know how much content is in a directory without opening a GUI interface.
In Bash:
du -cks * | sort -n | awk '\''BEGIN { split("KB,MB,GB,TB", Units, ","); } { u = 1;while ($1 >= 1024){$1 = $1 / 1024;u += 1;}$1 = sprintf("%.1f %s", $1, Units[u]);print $0;}'\'' | tail -11
I would suggest adding this to your bash aliases as ducks.
Output looks something like so:
~ > ducks
4.0 KB p
72.0 KB Music
24.1 MB Sites
35.0 MB Downloads
433.3 MB Dropbox
937.5 MB Movies
3.5 GB Library
6.3 GB Desktop
11.7 GB Documents
16.5 GB Pictures
39.5 GB total
~ > _
Update: My good friend Clayton suggested a much simpler way. The only problem is that the output is not sorted.
du -h -d 1
The output is the whole directory. I truncated the output to show the last 11.
~ > du -h -d 1 | tail -11
6.3G ./Desktop
12G ./Documents
212M ./Downloads
433M ./Dropbox
3.6G ./Library
8.0K ./Movies
72K ./Music
17G ./Pictures
0B ./Public
24M ./Sites
40G .
~ >
Update #2: Some systems do not accept the -d flag. This can be replaced with the --max-depth flag, like so:
du -h --max-depth 1
Sure you can load up a bulky editor and use its tools to do that, but why not do it in much faster in bash with awk.
In Bash:
awk '!x[$0]++' in.txt > out.txt
Wasn’t that easy?
Unicode does define some other characters which are sort of Mac-specific.
⌘ - ⌘ - ⌘ - the Command Key symbol
⌥ - ⌥ - ⌥ - the Option Key symbol
⇧ - ⇧ - ⇧ - the Shift Key symbol
⎋ - ⎋ - ⎋ - the Power Button
Note: The Power Button and Shift Key are not Mac-specific. The power button is described as “broken circle with northwest arrow”, or an escape character from ISO 9995-7. The shift key is described as an “outline up-arrow”.
Even though these are defined in standard Unicode, there is no guarantee that they will exist in the font of the receiving browser, but they’re at least globally defined, so they’re fair game.
If you haven’t figured out how Mac OS X 10.5 (Leopard) help search box allows you to search menu items, check out Chris Pirillo demoing the feature.
The problem for me is that I’m a keyboard person. Over 90% of my day, I barely touch the mouse. Now, to get to the help search bar with your keyboard, just press:
⌘ (Command) + ⇧ (Shift) + ? (Question Mark)
This is a quick one liner to find and replace strings in *nix. You can use this in your Mac terminal or Unix prompt.
In Bash:
find ./ -type f -exec sed -i 's/FIND/REPLACE/g' {} \;
Example:
find ./ -type f -exec sed -i 's/signin/login/g' {} \;
Hate the 2px difference you get when you apply a width on your input fields in Firefox?
Try this in CSS:
input, textarea, select{-moz-box-sizing: border-box;}
In bash:
ls -1 /Library/Logs/PanicReporter/ | wc -l
I’ve had 5.
In javascript:
window.scroll(0, window.document.height);