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

  • Home
  • SEARCH
  • 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 164721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:47:39+00:00 2026-05-11T11:47:39+00:00

My primary question is which approach is faster. Some briefing I’m developing an application

  • 0

My primary question is which approach is faster.

Some briefing

I’m developing an application using Mozilla.

I have this one module where I capture some data and store it in database. The data comes in intermittently. No duplicates are to be stored. For discussion sake we can assume a table with just one column, and let’s name that column is named ‘keyword’. So if we get a keyword that’s already in database we do not store it again. And yes, we have set this column as PRIMARY KEY and made it UNIQUE. 🙂

The query I have is:

1) Before I insert this new data into the database, shall I do a DB call and check if the keyword exists or not; if it doesn’t exists put it into DB? Something like:

function insert_keyword(keyword) {    if(!SQL.exists(keyword))    {        SQL.insert(keyword);    }  } 

OR

2) I just insert and let the database handle the condition, effectively letting it throw an exception, which I catch.

function insert_keyword(keyword) {     try {        SQL.insert(keyword);     }     catch (e)     {        // keyword exists!     } } 

I know catching an exception and not doing anything is bad! So I’m not looking for what is good and what is bad 🙂 . What I want to understand is which approach would be the fastest. (Specifically in relation to Mozilla, JavaScript and SQLite but general comments are welcomed!)

Sidenotes: The SQL. syntax I’ve used is just for illustration purpose. You may assume that I create SQL statements, execute them and fetch the result, or assume it is a JavaScript library call which does all the dirty work.

This question is a bit like this one:

Should I check for DB constraints in code or should I catch exceptions thrown by DB

But I want to understand the performance related differences specifically, as the application I’m working on needs to be as fast as possible (which application doesn’t? 😉 )

  • 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-11T11:47:40+00:00Added an answer on May 11, 2026 at 11:47 am

    Just tell your database to ignore duplicates (works only for columns that enforce unique values):

    INSERT OR IGNORE INTO table(keyword) VALUES('someWord'); 

    or create a column, that ignores duplicates.

    CREATE TABLE someTable(keyword PRIMARY KEY ON CONFLICT IGNORE); 

    Instead of ignore there are also those conflict clauses:

     ROLLBACK ABORT FAIL IGNORE REPLACE 

    For more information, read the page about conflict clauses of SQLite and the documentation of the INSERT statement.


    As to if it’s faster using exceptions or checking for the existance of values: Exceptions are expensive when raised and zero-cost when not raised. This means if you expect to have many duplicates use the check and if only a small number of keywords are duplicates use exceptions. After all, exceptions should be exceptional.

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

Sidebar

Ask A Question

Stats

  • Questions 138k
  • Answers 138k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Okay, I figured it out with a little more looking.… May 12, 2026 at 7:30 am
  • Editorial Team
    Editorial Team added an answer Similar to How to target a specific iPhone version? You… May 12, 2026 at 7:30 am
  • Editorial Team
    Editorial Team added an answer I would recommend changing RewriteCond %{HTTP_HOST} ^myopicvoid\.org$ RewriteCond %{REQUEST_URI} ^/$… May 12, 2026 at 7:30 am

Related Questions

My primary question is which approach is faster. Some briefing I'm developing an application
I'm working on an ASP.NET project that replaces many existing paper forms. One of
I have a data object (let's say it's called 'Entry') that has a set
I am creating the model for a web application. The tables have ID fields
I know, I quite dislike the catch-all survey type questions, but I couldn't think

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.