Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 152049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:33:14+00:00 2026-05-11T09:33:14+00:00

I am using an array with titles. Each titles index corresponds to an id

  • 0

I am using an array with titles. Each titles index corresponds to an id in a database which contains html for that given title.

Lets say I have a string which contains one of the titles.

title = 'why-birds-fly'; titles[] // an array which contains all the titles 

To use the string ‘title’ to get the corresponding id I could do:

for (i = 0; i < titles.length-1; i++) {   if (titles[i] == title)     return i+1; } 

Another method I could use is to create an associative array together with the titles array which is the exact opposite of titles. That is, it uses the string as an index and returns the number.

titles_id {blah:0,why-birds-fly:1,blah2:2} 

I could then access the ID by:

return titles_id[title]+1; 

What would be most effective considering CPU, Memory, etc?

Also, please let me know if my logic is all wrong.

Thanks Willem

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:33:15+00:00Added an answer on May 11, 2026 at 9:33 am

    The linear search approach has a complexity of O(n), and I think the worst case for the associative array approach is probably O(log n), (the best case maybe O(1) if the JS engine is using hashes and getting no collisions). It will depend on how a JS engine typically implements associative arrays/objects, but you can be sure it will beat O(n).

    So, the second approach will be faster, but will of course use more memory. This is a very typical trade off, gaining more speed, but using more memory, and only you can decide whether you want to make that trade.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my index file I get data from the database using this function (which
I am using Array() with each_with_index to output a array with index but I
I have an array that is populated using some xpath; so the items in
I'm using a PHP script to parse a HTML table into an array. But
I have a Ruby array which contains Post objects (a Mongoid model) with a
One thought is that perhaps I should create an array and just .each() all
I'm currently using Array.Copy to shift an array as such: Array.Copy(array, 0, array, 1,
I'm using Array.sample to return a random element from an array. I want to
I need to create an array of arrays. I have been using array_map(null,$a,$b,$c) to
Is there any speed/space/general performance gains in using array based implementation of a binary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.